/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 sources/mm/opencl.c

  • Committer: Suren A. Chilingaryan
  • Date: 2013-09-30 06:47:09 UTC
  • Revision ID: csa@dside.dyndns.org-20130930064709-55cde0k5ci76t8z5
Simple matrix multiplication

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
 
 
7
#define BLOCK_SIZE 16
 
8
 
 
9
#ifndef PPT
 
10
# define PPT 1
 
11
#endif 
 
12
 
 
13
#ifndef SHMEM
 
14
# define SHMEM 1
 
15
#endif
 
16
 
 
17
static cl_int err;
 
18
static cl_platform_id platform = 0;
 
19
static cl_device_id device = 0;
 
20
 
 
21
static cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
 
22
static cl_context ctx = 0;
 
23
static cl_command_queue queue = 0;
 
24
 
 
25
static cl_mem dev_res, dev_a, dev_b;
 
26
 
 
27
cl_program app;
 
28
cl_kernel kernel;
 
29
 
 
30
size_t matrix_size;
 
31
 
 
32
int exercise_required_alignment = PPT * BLOCK_SIZE;
 
33
 
 
34
int exercise_init(const char *name, size_t size) {
 
35
    int ret = 0;
 
36
    char param[1024];
 
37
 
 
38
    FILE *f;
 
39
    size_t len;
 
40
    char *source;
 
41
 
 
42
    err = clGetPlatformIDs(1, &platform, NULL);
 
43
    if (err != CL_SUCCESS) {
 
44
        printf( "clGetPlatformIDs() failed with %d\n", err );
 
45
        return 1;
 
46
    }
 
47
 
 
48
    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
 
49
    if (err != CL_SUCCESS) {
 
50
        printf( "clGetDeviceIDs() failed with %d\n", err );
 
51
        return 1;
 
52
    }
 
53
 
 
54
    err = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(param), param, NULL);
 
55
    if (!err) {
 
56
        printf("Using device: %s\n", param);
 
57
    }
 
58
 
 
59
 
 
60
    props[1] = (cl_context_properties)platform;
 
61
    ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
 
62
    if (err != CL_SUCCESS) {
 
63
        printf( "clCreateContext() failed with %d\n", err );
 
64
        return 1;
 
65
    }
 
66
 
 
67
    queue = clCreateCommandQueue(ctx, device, CL_QUEUE_PROFILING_ENABLE, &err);
 
68
    if (err != CL_SUCCESS) {
 
69
        printf( "clCreateCommandQueue() failed with %d\n", err );
 
70
        clReleaseContext(ctx);
 
71
        return 1;
 
72
    }
 
73
 
 
74
    dev_a = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
 
75
    if (err != CL_SUCCESS) {
 
76
        printf("clCreateBuffer() failed with %d\n", err);
 
77
        return 1;
 
78
    }
 
79
 
 
80
    dev_b = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
 
81
    if (err != CL_SUCCESS) {
 
82
        printf("clCreateBuffer() failed with %d\n", err);
 
83
        return 1;
 
84
    }
 
85
 
 
86
    dev_res = clCreateBuffer(ctx, CL_MEM_READ_WRITE, size * size * sizeof(float), NULL, &err);
 
87
    if (err != CL_SUCCESS) {
 
88
        printf("clCreateBuffer() failed with %d\n", err);
 
89
        return 1;
 
90
    }
 
91
 
 
92
    char *res = calloc(size * size, sizeof(float));
 
93
    if (res) {
 
94
        err = clEnqueueWriteBuffer(queue, dev_res, CL_TRUE, 0, size * size * sizeof(float), res, 0, NULL, NULL);
 
95
        if (err != CL_SUCCESS) {
 
96
            printf("clEnqueueWriteBuffer() failed with %d\n", err);
 
97
            return -1;
 
98
        }
 
99
        free(res);
 
100
 
 
101
        err = clFinish(queue);
 
102
        if (err != CL_SUCCESS) {
 
103
            printf("clFinish() failed with %d\n", err);
 
104
            return -1;
 
105
        }
 
106
    }
 
107
 
 
108
    sprintf(param, "%s.cl", name);
 
109
    f = fopen(param, "r");
 
110
    if (!f) {
 
111
        printf("Can't open file with OpenCL kernels\n");
 
112
        return 1;
 
113
    }
 
114
 
 
115
    fseek(f, 0, SEEK_END);
 
116
    len = ftell(f);
 
117
    fseek(f, 0, SEEK_SET);
 
118
    
 
119
    source = (char*)malloc(len + 128);
 
120
    if (!source) {
 
121
        printf("Can't allocate memory for OpenCL source\n");
 
122
        return 1;
 
123
    }
 
124
    
 
125
    sprintf(source, "#define BLOCK_SIZE %lu\n#define PPT %lu\n\n", BLOCK_SIZE, PPT, PPT);
 
126
    if (fread(source + strlen(source), 1, len, f) != len) {
 
127
        printf("Can't read OpenCL source\n");
 
128
        return 1;
 
129
    }
 
130
    fclose(f);
 
131
 
 
132
    len = strlen(source);
 
133
 
 
134
    app = clCreateProgramWithSource(ctx, 1, (const char**)&source, &len, &err);
 
135
    if (err != CL_SUCCESS) {
 
136
        printf("clCreateProgramWithSource() failed with %d\n", err);
 
137
        return 1;
 
138
    }
 
139
    
 
140
    err = clBuildProgram(app, 1, &device,  "", NULL, NULL);
 
141
    if (err != CL_SUCCESS) {
 
142
        printf("clBuildProgram() failed with %d\n", err);
 
143
        return 1;
 
144
    }
 
145
    
 
146
    free(source);
 
147
    
 
148
    kernel = clCreateKernel(app, "multiply", &err);
 
149
    if (err != CL_SUCCESS) {
 
150
        printf("clCreateKernel() failed with %d\n", err);
 
151
        return 1;
 
152
    }
 
153
 
 
154
    clSetKernelArg(kernel, 0, sizeof(cl_mem), &dev_res);
 
155
    clSetKernelArg(kernel, 1, sizeof(cl_mem), &dev_a);
 
156
    clSetKernelArg(kernel, 2, sizeof(cl_mem), &dev_b);
 
157
    matrix_size = size;
 
158
    clSetKernelArg(kernel, 3, sizeof(unsigned long), &matrix_size);
 
159
//    printf("%lu %lu %lu\n", PPT, PPT, 2 * PPT * PPT * BLOCK_SIZE * BLOCK_SIZE * sizeof(float) / 1024);
 
160
    err = clSetKernelArg(kernel, 4, 2 * SHMEM * BLOCK_SIZE * BLOCK_SIZE * sizeof(float), NULL);
 
161
 
 
162
    return 0;
 
163
}
 
164
 
 
165
int exercise_allocate(float **res, float **a, float **b, size_t size) {
 
166
    return 0;
 
167
}
 
168
 
 
169
 
 
170
void exercise_free() {
 
171
    clReleaseKernel(kernel);
 
172
    clReleaseProgram(app);
 
173
    
 
174
    clReleaseCommandQueue(queue);
 
175
    clReleaseContext(ctx);
 
176
 
 
177
    clReleaseMemObject(dev_res);
 
178
    clReleaseMemObject(dev_b);
 
179
    clReleaseMemObject(dev_a);
 
180
}
 
181
 
 
182
size_t exercise(float *res, float *a, float *b, size_t size, int iterations) {
 
183
    int i;
 
184
    
 
185
    cl_event event = NULL;
 
186
    size_t runtime = 0;
 
187
    cl_ulong start, end;
 
188
 
 
189
    size_t local_size[] = {BLOCK_SIZE, BLOCK_SIZE};
 
190
    size_t global_size[] = {size/PPT, size/PPT};
 
191
 
 
192
    for (i = 0; i < iterations; i++) {
 
193
        err = clEnqueueWriteBuffer(queue, dev_a, CL_TRUE, 0, size * size * sizeof(float), a, 0, NULL, NULL);
 
194
        if (err != CL_SUCCESS) {
 
195
            printf("clEnqueueWriteBuffer() failed with %d\n", err);
 
196
            return -11;
 
197
        }
 
198
 
 
199
        err = clEnqueueWriteBuffer(queue, dev_b, CL_TRUE, 0, size * size * sizeof(float), b, 0, NULL, NULL);
 
200
        if (err != CL_SUCCESS) {
 
201
            printf("clEnqueueWriteBuffer() failed with %d\n", err);
 
202
            return -1;
 
203
        }
 
204
 
 
205
        err = clEnqueueNDRangeKernel(queue, kernel, 2, 0, global_size, local_size, 0, NULL, &event);
 
206
        if (err != CL_SUCCESS) {
 
207
            printf("clEnqueueNDRangeKernel() failed with %d\n", err);
 
208
            return -1;
 
209
        }
 
210
 
 
211
        err = clEnqueueReadBuffer(queue, dev_res, CL_TRUE, 0, size * size * sizeof(float), res, 0, NULL, NULL);
 
212
        if (err != CL_SUCCESS) {
 
213
            printf("clEnqueueReadBuffer() failed with %d\n", err);
 
214
            return -1;
 
215
        }
 
216
 
 
217
        err = clFinish(queue);
 
218
        if (err != CL_SUCCESS) {
 
219
            printf("clFinish() failed with %d\n", err);
 
220
            return -1;
 
221
        }
 
222
        
 
223
        err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
 
224
        if (err != CL_SUCCESS) {
 
225
            printf("clGetEventProfilingInfo() failed with %d\n", err);
 
226
            return -1;
 
227
        }
 
228
        
 
229
        err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
 
230
        if (err != CL_SUCCESS) {
 
231
            printf("clGetEventProfilingInfo() failed with %d\n", err);
 
232
            return -1;
 
233
        }
 
234
        
 
235
        runtime += end - start;
 
236
    }
 
237
 
 
238
    return runtime;
 
239
}
 
240