/perf/kseta

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/kseta

« back to all changes in this revision

Viewing changes to tutorials/5_matrix/amdblas.c

  • Committer: Suren A. Chilingaryan
  • Date: 2013-10-08 23:53:50 UTC
  • Revision ID: csa@dside.dyndns.org-20131008235350-hsu8oukzkh05gtcm
Add tutorials

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <sys/types.h>
 
2
#include <stdio.h>
 
3
#include <string.h>
 
4
 
 
5
#include <CL/cl.h>
 
6
#include <clAmdBlas.h>
 
7
 
 
8
 
 
9
#ifndef PARALLEL
 
10
# define PARALLEL 1
 
11
#endif
 
12
 
 
13
static cl_int err;
 
14
static cl_platform_id platform = 0;
 
15
static cl_device_id device = 0;
 
16
 
 
17
static cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
 
18
static cl_context ctx = 0;
 
19
static cl_command_queue queue, queues[PARALLEL] = {0};
 
20
 
 
21
static cl_mem dev_res, dev_a, dev_b;
 
22
 
 
23
int exercise_required_alignment = 0;
 
24
 
 
25
int exercise_init(const char *name, size_t size) {
 
26
    int i;
 
27
    int ret = 0;
 
28
    char param[1024];
 
29
 
 
30
    err = clGetPlatformIDs(1, &platform, NULL);
 
31
    if (err != CL_SUCCESS) {
 
32
        printf( "clGetPlatformIDs() failed with %d\n", err );
 
33
        return 1;
 
34
    }
 
35
 
 
36
    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
 
37
    if (err != CL_SUCCESS) {
 
38
        printf( "clGetDeviceIDs() failed with %d\n", err );
 
39
        return 1;
 
40
    }
 
41
 
 
42
    err = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(param), param, NULL);
 
43
    if (!err) {
 
44
        printf("Using device: %s\n", param);
 
45
    }
 
46
 
 
47
 
 
48
    props[1] = (cl_context_properties)platform;
 
49
    ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
 
50
    if (err != CL_SUCCESS) {
 
51
        printf( "clCreateContext() failed with %d\n", err );
 
52
        return 1;
 
53
    }
 
54
 
 
55
    for (i = 0; i < PARALLEL; i++) {
 
56
        queues[i] = clCreateCommandQueue(ctx, device, CL_QUEUE_PROFILING_ENABLE, &err);
 
57
        if (err != CL_SUCCESS) {
 
58
            printf( "clCreateCommandQueue() failed with %d\n", err );
 
59
            clReleaseContext(ctx);
 
60
            return 1;
 
61
        }
 
62
    }
 
63
    queue = queues[0];
 
64
 
 
65
    err = clAmdBlasSetup();
 
66
    if (err != CL_SUCCESS) {
 
67
        printf("clAmdBlasSetup() failed with %d\n", err);
 
68
        clReleaseCommandQueue(queue);
 
69
        clReleaseContext(ctx);
 
70
        return 1;
 
71
    }
 
72
 
 
73
    dev_a = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
 
74
    if (err != CL_SUCCESS) {
 
75
        printf("clCreateBuffer() failed with %d\n", err);
 
76
        return 1;
 
77
    }
 
78
 
 
79
    dev_b = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
 
80
    if (err != CL_SUCCESS) {
 
81
        printf("clCreateBuffer() failed with %d\n", err);
 
82
        return 1;
 
83
    }
 
84
 
 
85
    dev_res = clCreateBuffer(ctx, CL_MEM_READ_WRITE, size * size * sizeof(float), NULL, &err);
 
86
    if (err != CL_SUCCESS) {
 
87
        printf("clCreateBuffer() failed with %d\n", err);
 
88
        return 1;
 
89
    }
 
90
    
 
91
    return 0;
 
92
}
 
93
 
 
94
int exercise_allocate(float **res, float **a, float **b, size_t size) {
 
95
    return  0;
 
96
}
 
97
 
 
98
void exercise_free() {
 
99
    int i;
 
100
    clAmdBlasTeardown();
 
101
 
 
102
    for (i = 0; i < PARALLEL; i++) {
 
103
        clReleaseCommandQueue(queues[i]);
 
104
    }
 
105
    clReleaseContext(ctx);
 
106
 
 
107
    clReleaseMemObject(dev_res);
 
108
    clReleaseMemObject(dev_b);
 
109
    clReleaseMemObject(dev_a);
 
110
}
 
111
 
 
112
size_t exercise(float *res, float *a, float *b, size_t size, int iterations) {
 
113
    int i,j;
 
114
 
 
115
    cl_event event = NULL;
 
116
    size_t runtime = 0;
 
117
    cl_ulong start, end;
 
118
    
 
119
    float alpha = 1, beta = 0;
 
120
    static const clAmdBlasTranspose transA = clAmdBlasNoTrans, transB = clAmdBlasNoTrans;
 
121
    static const clAmdBlasOrder order = clAmdBlasRowMajor;
 
122
 
 
123
    for (i = 0; i < iterations; i++) {
 
124
        err = clEnqueueWriteBuffer(queue, dev_a, CL_TRUE, 0, size * size * sizeof(float), a, 0, NULL, NULL);
 
125
        if (err != CL_SUCCESS) {
 
126
            printf("clEnqueueWriteBuffer() failed with %d\n", err);
 
127
            return -1;
 
128
        }
 
129
 
 
130
        err = clEnqueueWriteBuffer(queue, dev_b, CL_TRUE, 0, size * size * sizeof(float), b, 0, NULL, NULL);
 
131
        if (err != CL_SUCCESS) {
 
132
            printf("clEnqueueWriteBuffer() failed with %d\n", err);
 
133
            return -1;
 
134
        }
 
135
 
 
136
            // Memory corruption above 2 with current version of clBlas, i is set to 0
 
137
        err = clAmdBlasSgemm(order, transA, transB, size, size, size, alpha, dev_a, size, dev_b, size, beta, dev_res, size, PARALLEL, queues, 0, NULL, &event);
 
138
        if (err != CL_SUCCESS) {
 
139
            printf("clAmdBlasSgemm() failed with %d\n", err);
 
140
            return -1;
 
141
        }
 
142
 
 
143
        for (j = 0; j < PARALLEL; j++) {
 
144
            err = clFinish(queues[j]);
 
145
            if (err != CL_SUCCESS) {
 
146
                printf("clFinish() failed with %d\n", err);
 
147
                return -1;
 
148
            }
 
149
        }
 
150
 
 
151
        err = clEnqueueReadBuffer(queue, dev_res, CL_TRUE, 0, size * size * sizeof(float), res, 0, NULL, NULL);
 
152
        if (err != CL_SUCCESS) {
 
153
            printf("clEnqueueReadBuffer() failed with %d\n", err);
 
154
            return -1;
 
155
        }
 
156
        
 
157
        err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
 
158
        if (err != CL_SUCCESS) {
 
159
            printf("clGetEventProfilingInfo() failed with %d\n", err);
 
160
            return -1;
 
161
        }
 
162
        
 
163
        err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
 
164
        if (err != CL_SUCCESS) {
 
165
            printf("clGetEventProfilingInfo() failed with %d\n", err);
 
166
            return -1;
 
167
        }
 
168
        
 
169
        runtime += end - start;
 
170
    }
 
171
 
 
172
    return runtime;
 
173
}