AnsweredAssumed Answered

LLVM ERROR:call to an undefined function "memset"

Question asked by baifenghuang on Mar 15, 2018
Latest reply on Jun 1, 2018 by dipak

    Hello everyone, my programme crashes when I run it. The error message is title. The strange thing is I never use memset function in the kernel, but the error comes from clGetProgramBuildInfo。And when I delete the memset function in the host programme, it also comes to this error。

    I use AMD APP SDK 3.0,   visual studio 2017, windows 10 x64, AMD FirePro W7100。

    This is my host programme source code.

 

#include "stdafx.h"

#include<stdio.h>

#include <string.h>

#include <stdlib.h>

#include<CL/cl.h>

#include<setjmp.h>

#include<windows.h>

#define MATRIX_ROW 5888

#define MATRIX_COLUM 64

#define LINE_NUM 65

#define FILTER_ORDER 512

#define PRO_FILE "SA_DMAS.cl"

#define KERNEL_NAME "SA"

#define KERNEL_FILTER "FT"

#pragma warning( disable : 4996 )

#pragma comment (lib,"OpenCL.lib")

double dataTime = 0;

double computeTime1 = 0;

double computeTime2 = 0;

 

cl_context createContext(cl_device_id *device) {

cl_platform_id *platforms;

cl_context context;

cl_uint num_platforms;

cl_int error = 0;

error = clGetPlatformIDs(1, NULL, &num_platforms);

if (error != CL_SUCCESS) {

perror("Create platform failed!\n");

exit(EXIT_FAILURE);

}

platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platforms); 

error = clGetPlatformIDs(num_platforms, platforms, NULL);

 

 

error = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 1,

device, NULL);                              

if (error != CL_SUCCESS) {

perror("Create device failed!\n");

exit(EXIT_FAILURE);

}

context = clCreateContext(NULL, 1, device, NULL, NULL,

&error);                                

if (error != CL_SUCCESS) {

perror("Create context failed!\n");

exit(EXIT_FAILURE);

}

 

 

free(platforms);

return context;   

}

 

 

cl_command_queue createQueue(cl_context context,

cl_device_id device) {

cl_command_queue queue;

cl_int error;

queue = clCreateCommandQueue(context, device,

CL_QUEUE_PROFILING_ENABLE, &error);  

if (error != CL_SUCCESS) {

perror("Create queue failed!\n");

exit(EXIT_FAILURE);

}

return queue;

}

 

 

cl_program createProgram(cl_context context,

cl_device_id device) {

cl_program program;

FILE *program_handle;

size_t program_size;

char *program_buffer;

char *program_log;

size_t log_size;

int error = 0;

program_handle = fopen(PRO_FILE, "rb");

if (program_handle == NULL) {

perror("Open kernel file failed!\n");

exit(EXIT_FAILURE);

}

fseek(program_handle, 0, SEEK_END);

program_size = ftell(program_handle);

program_buffer = (char*)malloc(program_size + 1);

program_buffer[program_size] = '\0';

rewind(program_handle);

//fseek(program_handle, 0, SEEK_SET);

error = fread(program_buffer, sizeof(char), program_size,

program_handle);

if (error == 0) {

perror("Read kernel failed!\n");

exit(EXIT_FAILURE);

}

fclose(program_handle);

program = clCreateProgramWithSource(context, 1,

(const char **)&program_buffer, &program_size, &error);

if (error != CL_SUCCESS) {

perror("Create program failed!\n");

exit(EXIT_FAILURE);

}

free(program_buffer); 

error = clBuildProgram(program, 1, &device, "-Dsize=32",

NULL, NULL);

if (error != CL_SUCCESS) {

clGetProgramBuildInfo(program, device,

CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);

program_log = (char*)malloc(log_size + 1);

program_log[log_size] = '\0';

clGetProgramBuildInfo(program, device,

CL_PROGRAM_BUILD_LOG, log_size, program_log,

NULL);

printf("%s\n", program_log);

getchar();

free(program_log);

exit(EXIT_FAILURE);

}

return program;

}

 

int getData(const char *fileName, float **data_buffer) {

FILE *data_file;

size_t data_size;

long error;

data_file = fopen(fileName, "rb");

if (!data_file) {

printf("Open data file failed!\n");

return -1;

}

fseek(data_file, 0, SEEK_END);

data_size = ftell(data_file);

//fseek(data_file, 0, SEEK_SET);

rewind(data_file);

*data_buffer = (float *)malloc(data_size);

if (*data_buffer == NULL) {

printf("malloc filed!\n");

return -1;

}

memset((*data_buffer), 0, data_size);

error = fread(*data_buffer, sizeof(float),

data_size / sizeof(float), data_file);

if (error != data_size / sizeof(float)) {

printf("Read data file failed!\n");

return -1;

}

fclose(data_file);

return 0;

}

 

int siveResult(const char *fileName, float *data) {

FILE *writeFile;

cl_int error = 0;

 

 

 

writeFile = fopen(fileName, "wb");

if (!writeFile) {

printf("Open the file to write failed!\n");

return -1;

}

 

 

error = fwrite(data, sizeof(float), MATRIX_ROW * LINE_NUM, writeFile);

 

 

if (error != MATRIX_ROW * LINE_NUM) {

printf("Write data failed!\n");

return -1;

}

fclose(writeFile);            

return 0;

}

 

 

int main()

{

DWORD dwStart0 = GetTickCount();

cl_device_id device = NULL;

cl_context context = NULL;

cl_command_queue queue = NULL;

cl_program program = NULL;

cl_kernel sa_kernel = NULL;

cl_kernel FT_kernel = NULL;

const char *fileName = "data.bin";

const char *filterName = "filterCoe.bin";

const char *outputName = "writeData.bin";

float *data_buffer = NULL;

size_t data_size = 0;

 

 

cl_mem memObjects[4];

size_t global_size = 0;

size_t work_dims = 0;

size_t global_work_size[2] = { 0 };

size_t local_work_size[2] = { 0 };

// cl_int lineSize = 0;

cl_int error = 0;

// cl_event evt1;

// cl_event evt2;

cl_event evt3;

cl_int status;

cl_ulong timeStart = 0;

cl_ulong timeEnd = 0;

// DWORD c_cut_star;

// DWORD c_cut_end;

double c_cut_time = 0;

context = createContext(&device);

if (!device) {

perror("Get device failed!\n");

exit(EXIT_FAILURE);

}

if (!context) {

perror("Create context failed!\n");

exit(EXIT_FAILURE);

}

queue = createQueue(context, device);

if (!queue) {

perror("Create queue failed!\n");

exit(EXIT_FAILURE);;

}

program = createProgram(context, device);

if (!program) {

perror("Create program failed!\n");

exit(EXIT_FAILURE);

}

 

 

sa_kernel = clCreateKernel(program, KERNEL_NAME, &error);

FT_kernel = clCreateKernel(program, KERNEL_FILTER, &error);

if (error != CL_SUCCESS) {

//printf("%d\n", error);

perror("Create SA kernel failed!\n");

exit(EXIT_FAILURE);

}

 

 

 

/* clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS,

sizeof(global_size), &global_size, NULL);

clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE,

sizeof(local_size), &local_size, NULL);

global_size *= local_size;*/

 

 

 

float *input = (float*)malloc(MATRIX_ROW*MATRIX_COLUM*LINE_NUM * sizeof(float));

if (input == NULL) {

perror("Cannot allocate memory for input in host.");

exit(EXIT_FAILURE);

}

memset(input, 0, sizeof(input));

 

 

float *output = (float*)malloc(MATRIX_ROW*LINE_NUM * sizeof(float));

if (output == NULL) {

perror("Cannot allocate memory for output in host.");

exit(EXIT_FAILURE);

}

memset(output, 0, sizeof(output));

 

 

float *filter_coe = (float*)malloc(FILTER_ORDER * sizeof(float));

if (filter_coe == NULL) {

perror("Cannot allocate memory for filter_coe in host.");

exit(EXIT_FAILURE);

}

memset(filter_coe, 0, sizeof(filter_coe));

 

 

 

 

DWORD dwStart1 = GetTickCount();

error = getData(fileName, &input);

error = getData(filterName, &filter_coe);

 

//error = getData("filterCoe.bin", &filter_coe);

if (error != 0) {

perror("Get data failed!\n");

exit(EXIT_FAILURE);

}

 

 

 

memObjects[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,

sizeof(float)*MATRIX_ROW*LINE_NUM*MATRIX_COLUM, input, &error);

 

 

 

 

memObjects[1] = clCreateBuffer(context, CL_MEM_WRITE_ONLY,

sizeof(float)*MATRIX_ROW*LINE_NUM, NULL, &error);

 

 

memObjects[2] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,

sizeof(float)*FILTER_ORDER, filter_coe, &error);

 

// memObjects[3] = clCreateBuffer(context, CL_MEM_READ_WRITE,

//    sizeof(float)*MATRIX_ROW, NULL, &error);

 

 

if (memObjects[0] == NULL || memObjects[1] == NULL

|| memObjects[2] == NULL) {

printf("%d\n", error);

perror("Error creating memory objects!\n");

exit(EXIT_FAILURE);

}

 

 

error = clSetKernelArg(sa_kernel, 0, sizeof(cl_mem),

&memObjects[0]);

error |= clSetKernelArg(sa_kernel, 1, sizeof(cl_mem),

&memObjects[1]);

 

 

if (error != CL_SUCCESS) {

perror("Error setting sa_kernel arguments!\n");

exit(EXIT_FAILURE);

}

 

 

//global_size = MATRIX_DIM * FILETOTAL;//4597*65

work_dims = 2;

global_work_size[0] = MATRIX_ROW;

global_work_size[1] = LINE_NUM;

local_work_size[0] = 256;

local_work_size[1] = 1;

error = clEnqueueNDRangeKernel(queue, sa_kernel, work_dims, 0,

global_work_size, NULL, 0, NULL, &evt3);

 

 

if (error != CL_SUCCESS) {

perror("Error queuing sa_kernel for execution!\n");

getchar();

exit(EXIT_FAILURE);

}

 

clWaitForEvents(1, &evt3);

 

 

 

 

 

 

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_START, sizeof(timeStart),

&timeStart, NULL);

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_END, sizeof(timeEnd),

&timeEnd, NULL);

computeTime1 += (timeEnd - timeStart);

if (computeTime1 > 3.14*1e+38) {

printf("溢出2\n");

}

 

 

clReleaseEvent(evt3);

 

 

 

 

 

 

error |= clSetKernelArg(FT_kernel, 0, sizeof(cl_mem),

&memObjects[1]);

error |= clSetKernelArg(FT_kernel, 1, sizeof(cl_mem),

&memObjects[2]);

 

 

if (error != CL_SUCCESS) {

perror("Error setting FT_kernel arguments!\n");

exit(EXIT_FAILURE);

}

 

 

 

size_t FT_work_dims = 1;

size_t FT_global_work_size = 65;

error = clEnqueueNDRangeKernel(queue, FT_kernel, FT_work_dims, 0,

&FT_global_work_size, NULL, 0, NULL, &evt3);

 

 

clWaitForEvents(1, &evt3);

 

 

error = clGetEventInfo(evt3, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(status), &status, NULL);

if (error != CL_SUCCESS || status != CL_COMPLETE) {

printf("Running sa_kernel failed!\n");

getchar();

return  -1;

}

 

 

 

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_START, sizeof(timeStart),

&timeStart, NULL);

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_END, sizeof(timeEnd),

&timeEnd, NULL);

computeTime2 += (timeEnd - timeStart);

if (computeTime2 > 3.14*1e+38) {

printf("溢出2\n");

}

 

 

clReleaseEvent(evt3);

 

error = clEnqueueReadBuffer(queue, memObjects[1],

CL_TRUE, 0, MATRIX_ROW * LINE_NUM * sizeof(float),

output, 0, NULL, &evt3);

if (error != CL_SUCCESS) {

perror("Error reading result buffer!\n");

exit(EXIT_FAILURE);

}

 

clWaitForEvents(1, &evt3);

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_START, sizeof(timeStart),

&timeStart, NULL);

clGetEventProfilingInfo(evt3,

CL_PROFILING_COMMAND_END, sizeof(timeEnd),

&timeEnd, NULL);

dataTime += (timeEnd - timeStart);

clReleaseEvent(evt3);

 

 

 

 

 

 

 

 

 

 

 

error = siveResult(outputName, output);

if (error == 0)

printf("File %s calculated successed!\n",

fileName);

else

printf("File %s calculated failed!\n", fileName);

free(data_buffer);

 

 

DWORD dwEnd = GetTickCount();

printf("GPU->数据传输时间: %f\n", dataTime / 1000 / 1000);

printf("GPU->计算时间: %f\n", (computeTime1 + computeTime2) / 1000 / 1000);

printf("CPU->总共时间: %d\n", dwEnd - dwStart0);

printf("CPU->运行时间: %d\n", dwEnd - dwStart1);

printf("CPU->C运行时间: %f\n", c_cut_time);  //这条语句并没有起到作用

free(input);

free(output);

clReleaseProgram(program);

clReleaseContext(context);

clReleaseCommandQueue(queue);

clReleaseKernel(sa_kernel);

clReleaseMemObject(memObjects[0]);

clReleaseMemObject(memObjects[1]);

clReleaseMemObject(memObjects[2]);

getchar();

 

 

return 0;

}

Outcomes