Skip to content

Commit db069b1

Browse files
committed
create cl write_bffer exampe. matmul: factor out CL buffer.
1 parent 0b4b1bd commit db069b1

File tree

7 files changed

+358
-296
lines changed

7 files changed

+358
-296
lines changed

opencl/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1. [Vector type](vector_type.c)
1111
1. [clinfo](clinfo.c)
1212
1. [Preprocessor](matmul.c)
13+
1. [Write buffer](write_buffer.c)
1314
1. [Benchmark examples](benchmark-examples.md)
1415
1. [Matrix multiplication](matmul.c)
1516
1. Tools

opencl/common.h

+49-29
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,34 @@ typedef struct {
2323
cl_program program;
2424
} Common;
2525

26-
void common_init_options(
26+
char* common_read_file(const char *path) {
27+
char *buffer;
28+
FILE *f;
29+
long length;
30+
31+
f = fopen(path, "r");
32+
assert(NULL != f);
33+
fseek(f, 0, SEEK_END);
34+
length = ftell(f);
35+
fseek(f, 0, SEEK_SET);
36+
buffer = malloc(length + 1);
37+
if (fread(buffer, 1, length, f) < (size_t)length) {
38+
return NULL;
39+
}
40+
fclose(f);
41+
buffer[length] = '\0';
42+
return buffer;
43+
}
44+
45+
void common_create_kernel(
2746
Common *common,
2847
const char *source,
2948
const char *options
3049
) {
31-
char *err;
32-
size_t err_len;
3350
cl_int ret;
34-
cl_platform_id platform;
51+
char *err;
52+
size_t err_len;
3553

36-
clGetPlatformIDs(1, &platform, NULL);
37-
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &(common->device), NULL);
38-
common->context = clCreateContext(NULL, 1, &(common->device), NULL, NULL, NULL);
39-
common->command_queue = clCreateCommandQueue(common->context, common->device, 0, NULL);
4054
if (NULL != source) {
4155
common->program = clCreateProgramWithSource(common->context, 1, &source, NULL, NULL);
4256
ret = clBuildProgram(common->program, 1, &(common->device), options, NULL, NULL);
@@ -55,30 +69,36 @@ void common_init_options(
5569
}
5670
}
5771

58-
void common_init(
72+
void common_create_kernel_file(
5973
Common *common,
60-
const char *source
74+
const char *source_path,
75+
const char *options
6176
) {
62-
common_init_options(common, source, "");
77+
char *source;
78+
source = common_read_file(source_path);
79+
common_create_kernel(common, source, options);
80+
free(source);
6381
}
6482

65-
char* common_read_file(const char *path) {
66-
char *buffer;
67-
FILE *f;
68-
long length;
83+
void common_init_options(
84+
Common *common,
85+
const char *source,
86+
const char *options
87+
) {
88+
cl_platform_id platform;
6989

70-
f = fopen(path, "r");
71-
assert(NULL != f);
72-
fseek(f, 0, SEEK_END);
73-
length = ftell(f);
74-
fseek(f, 0, SEEK_SET);
75-
buffer = malloc(length + 1);
76-
if (fread(buffer, 1, length, f) < (size_t)length) {
77-
return NULL;
78-
}
79-
fclose(f);
80-
buffer[length] = '\0';
81-
return buffer;
90+
clGetPlatformIDs(1, &platform, NULL);
91+
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &(common->device), NULL);
92+
common->context = clCreateContext(NULL, 1, &(common->device), NULL, NULL, NULL);
93+
common->command_queue = clCreateCommandQueue(common->context, common->device, 0, NULL);
94+
common_create_kernel(common, source, options);
95+
}
96+
97+
void common_init(
98+
Common *common,
99+
const char *source
100+
) {
101+
common_init_options(common, source, "");
82102
}
83103

84104
void common_init_file_options(
@@ -96,7 +116,7 @@ void common_init_file(
96116
Common *common,
97117
const char *source_path
98118
) {
99-
common_init_file_options(common, source_path, "");
119+
common_init_file_options(common, source_path, "");
100120
}
101121

102122
void common_deinit(
@@ -111,7 +131,7 @@ void common_deinit(
111131
clReleaseContext(common->context);
112132
}
113133
#ifdef CL_1_2
114-
clReleaseDevice(common->device);
134+
clReleaseDevice(common->device);
115135
#endif
116136
}
117137

opencl/introduction.md

+4
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ OpenCL, like any other language has versions. As of 2013 the latest version is O
3535
- <http://stackoverflow.com/questions/33575715/opencl-opengl-interop-how-to-fill-a-climagegl>
3636

3737
Also see compute shaders for OpenGL 4.X, they seem to integrate better.
38+
39+
## Implement OpenGL with OpenCL:
40+
41+
- <https://github.com/a2flo/oclraster>

0 commit comments

Comments
 (0)