/perf/kseta

To get this branch, use:
bzr branch http://darksoft.org/webbzr/perf/kseta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <sys/types.h>
#include <stdio.h>
#include <string.h>

#include <CL/cl.h>

#define BLOCK_SIZE 16

#ifndef PPT
# define PPT 1
#endif 

#ifndef SHMEM
# define SHMEM 1
#endif

static cl_int err;
static cl_platform_id platform = 0;
static cl_device_id device = 0;

static cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
static cl_context ctx = 0;
static cl_command_queue queue = 0;

static cl_mem dev_res, dev_a, dev_b;

static cl_program app;
static cl_kernel kernel;

static size_t matrix_size;

int exercise_required_alignment = PPT * BLOCK_SIZE;

int exercise_init(const char *name, size_t size) {
    int ret = 0;
    char param[1024];

    FILE *f;
    size_t len;
    char *source;

    err = clGetPlatformIDs(1, &platform, NULL);
    if (err != CL_SUCCESS) {
        printf( "clGetPlatformIDs() failed with %d\n", err );
        return 1;
    }

    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    if (err != CL_SUCCESS) {
        printf( "clGetDeviceIDs() failed with %d\n", err );
        return 1;
    }

    err = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(param), param, NULL);
    if (!err) {
	printf("Using device: %s\n", param);
    }


    props[1] = (cl_context_properties)platform;
    ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
    if (err != CL_SUCCESS) {
        printf( "clCreateContext() failed with %d\n", err );
        return 1;
    }

    queue = clCreateCommandQueue(ctx, device, CL_QUEUE_PROFILING_ENABLE, &err);
    if (err != CL_SUCCESS) {
        printf( "clCreateCommandQueue() failed with %d\n", err );
        clReleaseContext(ctx);
        return 1;
    }

    dev_a = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
    if (err != CL_SUCCESS) {
	printf("clCreateBuffer() failed with %d\n", err);
	return 1;
    }

    dev_b = clCreateBuffer(ctx, CL_MEM_READ_ONLY, size * size * sizeof(float), NULL, &err);
    if (err != CL_SUCCESS) {
	printf("clCreateBuffer() failed with %d\n", err);
	return 1;
    }

    dev_res = clCreateBuffer(ctx, CL_MEM_READ_WRITE, size * size * sizeof(float), NULL, &err);
    if (err != CL_SUCCESS) {
	printf("clCreateBuffer() failed with %d\n", err);
	return 1;
    }

    char *res = calloc(size * size, sizeof(float));
    if (res) {
	err = clEnqueueWriteBuffer(queue, dev_res, CL_TRUE, 0, size * size * sizeof(float), res, 0, NULL, NULL);
	if (err != CL_SUCCESS) {
	    printf("clEnqueueWriteBuffer() failed with %d\n", err);
	    return -1;
	}
	free(res);

        err = clFinish(queue);
	if (err != CL_SUCCESS) {
	    printf("clFinish() failed with %d\n", err);
	    return -1;
	}
    }

    sprintf(param, "%s.cl", name);
    f = fopen(param, "r");
    if (!f) {
	printf("Can't open file with OpenCL kernels\n");
	return 1;
    }

    fseek(f, 0, SEEK_END);
    len = ftell(f);
    fseek(f, 0, SEEK_SET);
    
    source = (char*)malloc(len + 128);
    if (!source) {
	printf("Can't allocate memory for OpenCL source\n");
	return 1;
    }
    
    sprintf(source, "#define BLOCK_SIZE %u\n#define PPT %u\n\n", BLOCK_SIZE, PPT);
    if (fread(source + strlen(source), 1, len, f) != len) {
	printf("Can't read OpenCL source\n");
	return 1;
    }
    fclose(f);

    len = strlen(source);

    app = clCreateProgramWithSource(ctx, 1, (const char**)&source, &len, &err);
    if (err != CL_SUCCESS) {
	printf("clCreateProgramWithSource() failed with %d\n", err);
	return 1;
    }
    
    err = clBuildProgram(app, 1, &device,  "", NULL, NULL);
    if (err != CL_SUCCESS) {
	printf("clBuildProgram() failed with %d\n", err);
	return 1;
    }
    
    free(source);
    
    kernel = clCreateKernel(app, "multiply", &err);
    if (err != CL_SUCCESS) {
	printf("clCreateKernel() failed with %d\n", err);
	return 1;
    }

    clSetKernelArg(kernel, 0, sizeof(cl_mem), &dev_res);
    clSetKernelArg(kernel, 1, sizeof(cl_mem), &dev_a);
    clSetKernelArg(kernel, 2, sizeof(cl_mem), &dev_b);
    matrix_size = size;
    clSetKernelArg(kernel, 3, sizeof(unsigned long), &matrix_size);
//    printf("%lu %lu %lu\n", PPT, PPT, 2 * PPT * PPT * BLOCK_SIZE * BLOCK_SIZE * sizeof(float) / 1024);
    err = clSetKernelArg(kernel, 4, 2 * SHMEM * BLOCK_SIZE * BLOCK_SIZE * sizeof(float), NULL);

    return 0;
}

int exercise_allocate(float **res, float **a, float **b, size_t size) {
    return 0;
}


void exercise_free() {
    clReleaseKernel(kernel);
    clReleaseProgram(app);
    
    clReleaseCommandQueue(queue);
    clReleaseContext(ctx);

    clReleaseMemObject(dev_res);
    clReleaseMemObject(dev_b);
    clReleaseMemObject(dev_a);
}

size_t exercise(float *res, float *a, float *b, size_t size, int iterations) {
    int i;
    
    cl_event event = NULL;
    size_t runtime = 0;
    cl_ulong start, end;

    size_t local_size[] = {BLOCK_SIZE, BLOCK_SIZE};
    size_t global_size[] = {size/PPT, size/PPT};

    for (i = 0; i < iterations; i++) {
	err = clEnqueueWriteBuffer(queue, dev_a, CL_TRUE, 0, size * size * sizeof(float), a, 0, NULL, NULL);
	if (err != CL_SUCCESS) {
	    printf("clEnqueueWriteBuffer() failed with %d\n", err);
	    return -11;
	}

	err = clEnqueueWriteBuffer(queue, dev_b, CL_TRUE, 0, size * size * sizeof(float), b, 0, NULL, NULL);
	if (err != CL_SUCCESS) {
	    printf("clEnqueueWriteBuffer() failed with %d\n", err);
	    return -1;
	}

	err = clEnqueueNDRangeKernel(queue, kernel, 2, 0, global_size, local_size, 0, NULL, &event);
	if (err != CL_SUCCESS) {
	    printf("clEnqueueNDRangeKernel() failed with %d\n", err);
	    return -1;
	}

	err = clEnqueueReadBuffer(queue, dev_res, CL_TRUE, 0, size * size * sizeof(float), res, 0, NULL, NULL);
	if (err != CL_SUCCESS) {
	    printf("clEnqueueReadBuffer() failed with %d\n", err);
	    return -1;
	}

	err = clFinish(queue);
	if (err != CL_SUCCESS) {
	    printf("clFinish() failed with %d\n", err);
	    return -1;
	}
	
	err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
	if (err != CL_SUCCESS) {
	    printf("clGetEventProfilingInfo() failed with %d\n", err);
	    return -1;
	}
	
	err = clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
	if (err != CL_SUCCESS) {
	    printf("clGetEventProfilingInfo() failed with %d\n", err);
	    return -1;
	}
	
	runtime += end - start;
    }

    return runtime;
}