Skip to content

Commit 98758fb

Browse files
committed
opencl: Use std::vector and clean code
Signed-off-by: Stefan Weil <[email protected]>
1 parent 53795a8 commit 98758fb

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/opencl/openclwrapper.cpp

+17-31
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,6 @@ static cl_mem allocateZeroCopyBuffer(const KernelEnv& rEnv,
667667
static Pix* mapOutputCLBuffer(const KernelEnv& rEnv, cl_mem clbuffer, Pix* pixd,
668668
Pix* pixs, int elements, cl_mem_flags flags,
669669
bool memcopy = false, bool sync = true) {
670-
PROCNAME("mapOutputCLBuffer");
671670
if (!pixd) {
672671
if (memcopy) {
673672
if ((pixd = pixCreateTemplate(pixs)) == nullptr)
@@ -891,35 +890,33 @@ int OpenclDevice::WriteBinaryToFile(const char* fileName, const char* birary,
891890

892891
return 1;
893892
}
893+
894894
int OpenclDevice::GeneratBinFromKernelSource(cl_program program,
895895
const char* clFileName) {
896896
unsigned int i = 0;
897897
cl_int clStatus;
898-
size_t* binarySizes;
899898
cl_uint numDevices;
900-
cl_device_id* mpArryDevsID;
901899
char* str = nullptr;
902900

903901
clStatus = clGetProgramInfo(program, CL_PROGRAM_NUM_DEVICES,
904902
sizeof(numDevices), &numDevices, nullptr);
905903
CHECK_OPENCL(clStatus, "clGetProgramInfo");
906904

907-
mpArryDevsID = (cl_device_id*)malloc(sizeof(cl_device_id) * numDevices);
908-
if (mpArryDevsID == nullptr) {
909-
return 0;
910-
}
905+
std::vector<cl_device_id> mpArryDevsID(numDevices);
906+
911907
/* grab the handles to all of the devices in the program. */
912908
clStatus = clGetProgramInfo(program, CL_PROGRAM_DEVICES,
913-
sizeof(cl_device_id) * numDevices, mpArryDevsID,
909+
sizeof(cl_device_id) * numDevices,
910+
&mpArryDevsID[0],
914911
nullptr);
915912
CHECK_OPENCL(clStatus, "clGetProgramInfo");
916913

917914
/* figure out the sizes of each of the binaries. */
918-
binarySizes = (size_t*)malloc(sizeof(size_t) * numDevices);
915+
std::vector<size_t> binarySizes(numDevices);
919916

920917
clStatus =
921918
clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
922-
sizeof(size_t) * numDevices, binarySizes, nullptr);
919+
sizeof(size_t) * numDevices, &binarySizes[0], nullptr);
923920
CHECK_OPENCL(clStatus, "clGetProgramInfo");
924921

925922
/* copy over all of the generated binaries. */
@@ -969,23 +966,15 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program,
969966
free(binaries[i]);
970967
}
971968

972-
free(binarySizes);
973-
binarySizes = nullptr;
974-
975-
free(mpArryDevsID);
976-
mpArryDevsID = nullptr;
977-
978969
return 1;
979970
}
980971

981972
int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
982973
// PERF_COUNT_START("CompileKernelFile")
983974
cl_int clStatus = 0;
984-
size_t length;
985-
char* buildLog = nullptr;
986975
const char* source;
987976
size_t source_size[1];
988-
int b_error, binary_status, binaryExisted, idx;
977+
int binary_status, binaryExisted, idx;
989978
cl_uint numDevices;
990979
FILE *fd, *fd1;
991980
const char* filename = "kernel.cl";
@@ -1010,10 +999,10 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
1010999

10111000
std::vector<cl_device_id> mpArryDevsID(numDevices);
10121001
// PERF_COUNT_SUB("get numDevices")
1013-
b_error = 0;
1014-
length = 0;
1015-
b_error |= fseek(fd, 0, SEEK_END) < 0;
1016-
b_error |= (length = ftell(fd)) <= 0;
1002+
bool b_error = fseek(fd, 0, SEEK_END) < 0;
1003+
long pos = ftell(fd);
1004+
b_error |= (pos <= 0);
1005+
size_t length = pos;
10171006
b_error |= fseek(fd, 0, SEEK_SET) < 0;
10181007
if (b_error) {
10191008
return 0;
@@ -1072,6 +1061,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10721061
PERF_COUNT_END
10731062
if (clStatus != CL_SUCCESS) {
10741063
printf("BuildProgram error!\n");
1064+
size_t length;
10751065
if (!gpuInfo->mnIsUserCreated) {
10761066
clStatus = clGetProgramBuildInfo(
10771067
gpuInfo->mpArryPrograms[idx], gpuInfo->mpArryDevsID[0],
@@ -1085,18 +1075,15 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10851075
printf("opencl create build log fail\n");
10861076
return 0;
10871077
}
1088-
buildLog = (char*)malloc(length);
1089-
if (buildLog == (char*)nullptr) {
1090-
return 0;
1091-
}
1078+
std::vector<char> buildLog(length);
10921079
if (!gpuInfo->mnIsUserCreated) {
10931080
clStatus = clGetProgramBuildInfo(
10941081
gpuInfo->mpArryPrograms[idx], gpuInfo->mpArryDevsID[0],
1095-
CL_PROGRAM_BUILD_LOG, length, buildLog, &length);
1082+
CL_PROGRAM_BUILD_LOG, length, &buildLog[0], &length);
10961083
} else {
10971084
clStatus = clGetProgramBuildInfo(gpuInfo->mpArryPrograms[idx],
10981085
gpuInfo->mpDevID, CL_PROGRAM_BUILD_LOG,
1099-
length, buildLog, &length);
1086+
length, &buildLog[0], &length);
11001087
}
11011088
if (clStatus != CL_SUCCESS) {
11021089
printf("opencl program build info fail\n");
@@ -1105,11 +1092,10 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
11051092

11061093
fd1 = fopen("kernel-build.log", "w+");
11071094
if (fd1 != nullptr) {
1108-
fwrite(buildLog, sizeof(char), length, fd1);
1095+
fwrite(&buildLog[0], sizeof(char), length, fd1);
11091096
fclose(fd1);
11101097
}
11111098

1112-
free(buildLog);
11131099
// PERF_COUNT_SUB("build error log")
11141100
return 0;
11151101
}

0 commit comments

Comments
 (0)