// get .cl root path char* HGPU_io_path_get_root(void){ HGPU_io_path_get(&HGPU_io_path_current); HGPU_io_path_get(&HGPU_io_path_root); char* j_prev = HGPU_io_path_root; char* j_cur = HGPU_io_path_root; bool flag_found = false; while((!flag_found)&&(j_cur)){ sprintf_s(HGPU_io_path_root,HGPU_FILENAME_MAX,"%s%s",HGPU_io_path_current, HGPU_PATH_SEPARATOR_LINUX); j_cur = strstr(j_prev+1, HGPU_PATH_SEPARATOR_LINUX); if((j_cur) && (j_cur>j_prev)) { j_prev=j_cur; sprintf_s(j_cur+1,HGPU_FILENAME_MAX-(j_cur+1-HGPU_io_path_root),"%s",HGPU_GPU_ROOT_FILE); if (HGPU_io_file_check_existence(HGPU_io_path_root)) { sprintf_s(j_cur,HGPU_FILENAME_MAX-(j_cur+1-HGPU_io_path_root),"%s", HGPU_PATH_SEPARATOR_LINUX); flag_found = true; } } } if (!flag_found) { HGPU_io_path_root[0] = 0; HGPU_error_message(HGPU_ERROR_PATH_NOT_FOUND_ROOT,"could not determine OpenCL kernels root path (file hgpucl.txt not found)"); } return HGPU_io_path_root; }
// get path void HGPU_io_path_get(char** path){ if (!path) HGPU_error(HGPU_ERROR_ARRAY_OUT_OF_BOUND); size_t path_length = HGPU_FILENAME_MAX; char* path_new = *path; if (!path_new) HGPU_string_resize(&path_new,path_length); if (!GetCurrentDir(path_new,(int) path_length)) HGPU_error_message(HGPU_ERROR_DEVICE_INITIALIZATION_FAILED,"could not obtain inf path"); HGPU_io_path_set_separator(&path_new,path_length); *path = path_new; }
// write buffer into binary file void HGPU_io_file_write_binary(const char* file_name,const unsigned char* buffer,const size_t bytes_to_write){ FILE* file_for_write; fopen_s(&file_for_write,file_name,"wb"); if(file_for_write){ fwrite(buffer,sizeof(unsigned char),bytes_to_write,file_for_write); if (fclose(file_for_write)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); } else HGPU_error_message(HGPU_ERROR_FILE_NOT_FOUND,"error writting binary file"); }
static void HGPU_PRNG_RANMAR_init_GPU(HGPU_GPU_context* context,void* PRNG_state,HGPU_PRNG_parameters* PRNG_parameters){ HGPU_PRNG_RANMAR_state_t* state = (HGPU_PRNG_RANMAR_state_t*) PRNG_state; size_t seeds_size = HGPU_GPU_context_buffer_size_align(context,PRNG_parameters->instances * 2); size_t seed_table_size = HGPU_GPU_context_buffer_size_align(context,PRNG_parameters->instances * 2 * (97+1)); // 97 - size of seed table, 1 - size of indices size_t randoms_size = HGPU_GPU_context_buffer_size_align(context,PRNG_parameters->instances * PRNG_parameters->samples); cl_uint4* PRNG_seeds = (cl_uint4*) calloc(seeds_size,sizeof(cl_uint4)); cl_float4* PRNG_seed_table_float4= (cl_float4*) calloc(seed_table_size,sizeof(cl_float4)); cl_float4* PRNG_randoms = NULL; cl_double4* PRNG_randoms_double = NULL; if (PRNG_parameters->precision==HGPU_precision_double) PRNG_randoms_double = (cl_double4*) calloc(randoms_size,sizeof(cl_double4)); else PRNG_randoms = (cl_float4*) calloc(randoms_size,sizeof(cl_float4)); if ((!PRNG_seeds) || (!PRNG_seed_table_float4) || ((!PRNG_randoms_double) && (!PRNG_randoms))) HGPU_error_message(HGPU_ERROR_NO_MEMORY,"could not allocate memory for randoms"); for (unsigned int i=0; i<(seeds_size>>1); i++) { PRNG_seeds[i].s[0] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_1; PRNG_seeds[i].s[1] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_1; PRNG_seeds[i].s[2] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_1; PRNG_seeds[i].s[3] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_1; } for (unsigned int i=(seeds_size>>1); i<seeds_size; i++) { PRNG_seeds[i].s[0] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_2; PRNG_seeds[i].s[1] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_2; PRNG_seeds[i].s[2] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_2; PRNG_seeds[i].s[3] = HGPU_PRNG_rand32bit() % HGPU_PRNG_RANMAR_init_2; } PRNG_seeds[0].s[0] = state->seed1; // setup first thread as CPU PRNG_seeds[PRNG_parameters->instances].s[0] = state->seed2; // setup first thread as CPU unsigned int seeds_id = 0; unsigned int seed_table_id = 0; unsigned int randoms_id = 0; seeds_id = HGPU_GPU_context_buffer_init(context,PRNG_seeds,HGPU_GPU_buffer_type_io,seeds_size,sizeof(cl_uint4)); HGPU_GPU_context_buffer_set_name(context,seeds_id,(char*) "(RANMAR) PRNG_input_seeds"); seed_table_id = HGPU_GPU_context_buffer_init(context,PRNG_seed_table_float4,HGPU_GPU_buffer_type_io,seed_table_size,sizeof(cl_float4)); HGPU_GPU_context_buffer_set_name(context,seed_table_id,(char*) "(RANMAR) PRNG_seed_table"); if (PRNG_parameters->precision==HGPU_precision_double) randoms_id = HGPU_GPU_context_buffer_init(context,PRNG_randoms_double,HGPU_GPU_buffer_type_io,randoms_size,sizeof(cl_double4)); else randoms_id = HGPU_GPU_context_buffer_init(context,PRNG_randoms,HGPU_GPU_buffer_type_io,randoms_size,sizeof(cl_float4)); HGPU_GPU_context_buffer_set_name(context,randoms_id,(char*) "(RANMAR) PRNG_randoms"); PRNG_parameters->id_buffer_input_seeds = seeds_id; PRNG_parameters->id_buffer_seeds = seed_table_id; PRNG_parameters->id_buffer_randoms = randoms_id; }
// write buffer into file with path void HGPU_io_file_write_with_path(const char* file_path,const char* file_name,const char* buffer){ char* filename_with_path = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if (!filename_with_path) HGPU_error(HGPU_ERROR_NO_MEMORY); HGPU_io_path_join_filename(&filename_with_path,HGPU_FILENAME_MAX,file_path,file_name); FILE* file_for_write; fopen_s(&file_for_write,filename_with_path,"w+"); free(filename_with_path); if(file_for_write){ fprintf(file_for_write,buffer); if (fclose(file_for_write)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); } else HGPU_error_message(HGPU_ERROR_FILE_NOT_FOUND,"error writting file"); }
// read file into buffer char* HGPU_io_file_read(const char* file_name){ FILE* file_to_read; char buffer[HGPU_FILENAME_MAX]; sprintf_s(buffer,sizeof(buffer),"%s",file_name); fopen_s(&file_to_read,buffer,"r"); if(!file_to_read){ HGPU_error_message(HGPU_ERROR_FILE_NOT_FOUND,".cl-file not found"); return NULL; } fseek (file_to_read, 0, SEEK_END); unsigned int file_length = ftell(file_to_read); fseek (file_to_read, 0 , SEEK_SET); char* cl_kernels_source = (char*) calloc(file_length + 2, sizeof(char)); if (!cl_kernels_source) HGPU_error(HGPU_ERROR_NO_MEMORY); unsigned int file_read_length = (unsigned int) fread(cl_kernels_source, sizeof(char), file_length, file_to_read); if (!file_read_length) free(cl_kernels_source); if (fclose(file_to_read)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); return cl_kernels_source; }
static void HGPU_PRNG_CONSTANT_init_GPU(HGPU_GPU_context* context,void*,HGPU_PRNG_parameters* PRNG_parameters){ size_t randoms_size = HGPU_GPU_context_buffer_size_align(context,PRNG_parameters->instances * PRNG_parameters->samples); cl_float4* PRNG_randoms = NULL; cl_double4* PRNG_randoms_double = NULL; if (PRNG_parameters->precision==HGPU_precision_double) PRNG_randoms_double = (cl_double4*) calloc(randoms_size,sizeof(cl_double4)); else PRNG_randoms = (cl_float4*) calloc(randoms_size,sizeof(cl_float4)); if ((!PRNG_randoms_double) && (!PRNG_randoms)) HGPU_error_message(HGPU_ERROR_NO_MEMORY,"could not allocate memory for randoms"); unsigned int randoms_id = 0; if (PRNG_parameters->precision==HGPU_precision_double) randoms_id = HGPU_GPU_context_buffer_init(context,PRNG_randoms_double,HGPU_GPU_buffer_type_io,randoms_size,sizeof(cl_double4)); else randoms_id = HGPU_GPU_context_buffer_init(context,PRNG_randoms,HGPU_GPU_buffer_type_io,randoms_size,sizeof(cl_float4)); HGPU_GPU_context_buffer_set_name(context,randoms_id,(char*) "(CONSTANT) PRNG_randoms"); PRNG_parameters->id_buffer_input_seeds = HGPU_GPU_MAX_BUFFERS; PRNG_parameters->id_buffer_seeds = HGPU_GPU_MAX_BUFFERS; PRNG_parameters->id_buffer_randoms = randoms_id; }