Mercurial > hg > Gears > GearsAgda
changeset 304:9755206813cb
helper_string.h for ANSI C
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Feb 2017 11:36:41 +0900 |
parents | 1dbaef86593b |
children | ec0a5b4fba05 |
files | src/parallel_execution/CMakeLists.txt src/parallel_execution/CUDAWorker.cbc src/parallel_execution/context.h src/parallel_execution/helper_cuda.h src/parallel_execution/helper_string.h |
diffstat | 5 files changed, 27 insertions(+), 288 deletions(-) [+] |
line wrap: on
line diff
--- a/src/parallel_execution/CMakeLists.txt Mon Feb 13 18:23:29 2017 +0900 +++ b/src/parallel_execution/CMakeLists.txt Tue Feb 14 11:36:41 2017 +0900 @@ -12,7 +12,7 @@ set(NVCCFLAG "-std=c++11" "-g" "-O0" ) set(CUDA_LINK_FLAGS "-framework CUDA -lc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names /Developer/NVIDIA/CUDA-8.0/lib/libcudart_static.a -Wl,-rpath,/usr/local/cuda/lib") find_package(CUDA REQUIRED) - add_definitions("-Wall -g -DUSE_CUDAWorker") + add_definitions("-Wall -g -DUSE_CUDAWorker=1") else() add_definitions("-Wall -g") endif()
--- a/src/parallel_execution/CUDAWorker.cbc Mon Feb 13 18:23:29 2017 +0900 +++ b/src/parallel_execution/CUDAWorker.cbc Tue Feb 14 11:36:41 2017 +0900 @@ -2,19 +2,16 @@ #include <sys/time.h> #include <string.h> #include <stdlib.h> - +#include <libkern/OSAtomic.h> #include <cuda.h> - #include <cuda_runtime.h> #include "helper_cuda.h" -#include <libkern/OSAtomic.h> - #include "../context.h" static void start_CUDAworker(Worker* worker); -union Data* createCUDAWorker(struct Context* context, int id, Queue* queue) { +Worker* createCUDAWorker(struct Context* context, int id, Queue* queue) { struct Worker* worker = ALLOC(context, Worker); struct CUDAWorker* CUDAWorker = ALLOC(context, CUDAWorker); worker->worker = (union Data*)CUDAWorker; @@ -23,7 +20,7 @@ worker->taskReceive = C_taskReceiveCUDAWorker; worker->shutdown = C_shutdownCUDAWorker; pthread_create(&worker->worker->CUDAWorker.thread, NULL, (void*)&start_CUDAworker, worker); - return (union Data*)(worker); + return worker; } static void start_CUDAworker(Worker* worker) {
--- a/src/parallel_execution/context.h Mon Feb 13 18:23:29 2017 +0900 +++ b/src/parallel_execution/context.h Tue Feb 14 11:36:41 2017 +0900 @@ -3,7 +3,7 @@ #define CONTEXT_H #include <stdlib.h> #include <pthread.h> -#ifdef USE_CUDA +#ifdef USE_CUDAWorker #include <cuda.h> #endif
--- a/src/parallel_execution/helper_cuda.h Mon Feb 13 18:23:29 2017 +0900 +++ b/src/parallel_execution/helper_cuda.h Tue Feb 14 11:36:41 2017 +0900 @@ -288,9 +288,7 @@ return "<unknown>"; } -#endif - -#ifdef __cuda_cuda_h__ +#else // CUDA Driver API errors const char *_cudaGetErrorEnum(CUresult error) { @@ -981,18 +979,31 @@ #endif #endif -template< typename T > -void check(T result, char const *const func, const char *const file, int const line) +#ifndef __DRIVER_TYPES_H__ +static inline void check(CUresult result, char const *const func, const char *const file, int const line) { if (result) { fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", - file, line, static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func); + file, line, (unsigned int)(result), _cudaGetErrorEnum(result), func); DEVICE_RESET // Make sure we call CUDA Device Reset before exiting exit(EXIT_FAILURE); } } +#else +static inline void check(cudaError_t result, char const *const func, const char *const file, int const line) +{ + if (result) + { + fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", + file, line, (unsigned int)(result), _cudaGetErrorEnum(result), func); + DEVICE_RESET + // Make sure we call CUDA Device Reset before exiting + exit(EXIT_FAILURE); + } +} +#endif #ifdef __DRIVER_TYPES_H__ // This will output the proper CUDA error strings in the event that a CUDA host call returns an error @@ -1029,7 +1040,7 @@ inline int _ConvertSMVer2Cores(int major, int minor) { // Defines for GPU Architecture types (using the SM version to determine the # of cores per SM - typedef struct + typedef struct sSMtoCores { int SM; // 0xMm (hexidecimal notation), M = SM Major version, and m = SM minor version int Cores; @@ -1097,7 +1108,7 @@ return -devID; } - cudaDeviceProp deviceProp; + struct cudaDeviceProp deviceProp; checkCudaErrors(cudaGetDeviceProperties(&deviceProp, devID)); if (deviceProp.computeMode == cudaComputeModeProhibited) @@ -1127,7 +1138,7 @@ int devices_prohibited = 0; unsigned long long max_compute_perf = 0; - cudaDeviceProp deviceProp; + struct cudaDeviceProp deviceProp; cudaGetDeviceCount(&device_count); checkCudaErrors(cudaGetDeviceCount(&device_count)); @@ -1216,7 +1227,7 @@ // Initialization code to find the best CUDA Device inline int findCudaDevice(int argc, const char **argv) { - cudaDeviceProp deviceProp; + struct cudaDeviceProp deviceProp; int devID = 0; // If the command-line has a device number specified, use it @@ -1255,7 +1266,7 @@ // General check for CUDA GPU SM Capabilities inline bool checkCudaCapabilities(int major_version, int minor_version) { - cudaDeviceProp deviceProp; + struct cudaDeviceProp deviceProp; deviceProp.major = 0; deviceProp.minor = 0; int dev;
--- a/src/parallel_execution/helper_string.h Mon Feb 13 18:23:29 2017 +0900 +++ b/src/parallel_execution/helper_string.h Tue Feb 14 11:36:41 2017 +0900 @@ -15,7 +15,6 @@ #include <stdio.h> #include <stdlib.h> -#include <fstream.h> #include <string.h> #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) @@ -142,36 +141,6 @@ return bFound; } -// This function wraps the CUDA Driver API into a template function -template <class T> -inline bool getCmdLineArgumentValue(const int argc, const char **argv, const char *string_ref, T *value) -{ - bool bFound = false; - - if (argc >= 1) - { - for (int i=1; i < argc; i++) - { - int string_start = stringRemoveDelimiter('-', argv[i]); - const char *string_argv = &argv[i][string_start]; - int length = (int)strlen(string_ref); - - if (!STRNCASECMP(string_argv, string_ref, length)) - { - if (length+1 <= (int)strlen(string_argv)) - { - int auto_inc = (string_argv[length] == '=') ? 1 : 0; - *value = (T)atoi(&string_argv[length + auto_inc]); - } - - bFound = true; - i=argc; - } - } - } - - return bFound; -} inline int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref) { @@ -285,243 +254,5 @@ return bFound; } -////////////////////////////////////////////////////////////////////////////// -//! Find the path for a file assuming that -//! files are found in the searchPath. -//! -//! @return the path if succeeded, otherwise 0 -//! @param filename name of the file -//! @param executable_path optional absolute path of the executable -////////////////////////////////////////////////////////////////////////////// -inline char *sdkFindFilePath(const char *filename, const char *executable_path) -{ - // <executable_name> defines a variable that is replaced with the name of the executable - - // Typical relative search paths to locate needed companion files (e.g. sample input data, or JIT source files) - // The origin for the relative search may be the .exe file, a .bat file launching an .exe, a browser .exe launching the .exe or .bat, etc - const char *searchPath[] = - { - "./", // same dir - "./<executable_name>_data_files/", - "./common/", // "/common/" subdir - "./common/data/", // "/common/data/" subdir - "./data/", // "/data/" subdir - "./src/", // "/src/" subdir - "./src/<executable_name>/data/", // "/src/<executable_name>/data/" subdir - "./inc/", // "/inc/" subdir - "./0_Simple/", // "/0_Simple/" subdir - "./1_Utilities/", // "/1_Utilities/" subdir - "./2_Graphics/", // "/2_Graphics/" subdir - "./3_Imaging/", // "/3_Imaging/" subdir - "./4_Finance/", // "/4_Finance/" subdir - "./5_Simulations/", // "/5_Simulations/" subdir - "./6_Advanced/", // "/6_Advanced/" subdir - "./7_CUDALibraries/", // "/7_CUDALibraries/" subdir - "./8_Android/", // "/8_Android/" subdir - "./samples/", // "/samples/" subdir - - "./0_Simple/<executable_name>/data/", // "/0_Simple/<executable_name>/data/" subdir - "./1_Utilities/<executable_name>/data/", // "/1_Utilities/<executable_name>/data/" subdir - "./2_Graphics/<executable_name>/data/", // "/2_Graphics/<executable_name>/data/" subdir - "./3_Imaging/<executable_name>/data/", // "/3_Imaging/<executable_name>/data/" subdir - "./4_Finance/<executable_name>/data/", // "/4_Finance/<executable_name>/data/" subdir - "./5_Simulations/<executable_name>/data/", // "/5_Simulations/<executable_name>/data/" subdir - "./6_Advanced/<executable_name>/data/", // "/6_Advanced/<executable_name>/data/" subdir - "./7_CUDALibraries/<executable_name>/", // "/7_CUDALibraries/<executable_name>/" subdir - "./7_CUDALibraries/<executable_name>/data/", // "/7_CUDALibraries/<executable_name>/data/" subdir - - "../", // up 1 in tree - "../common/", // up 1 in tree, "/common/" subdir - "../common/data/", // up 1 in tree, "/common/data/" subdir - "../data/", // up 1 in tree, "/data/" subdir - "../src/", // up 1 in tree, "/src/" subdir - "../inc/", // up 1 in tree, "/inc/" subdir - - "../0_Simple/<executable_name>/data/", // up 1 in tree, "/0_Simple/<executable_name>/" subdir - "../1_Utilities/<executable_name>/data/", // up 1 in tree, "/1_Utilities/<executable_name>/" subdir - "../2_Graphics/<executable_name>/data/", // up 1 in tree, "/2_Graphics/<executable_name>/" subdir - "../3_Imaging/<executable_name>/data/", // up 1 in tree, "/3_Imaging/<executable_name>/" subdir - "../4_Finance/<executable_name>/data/", // up 1 in tree, "/4_Finance/<executable_name>/" subdir - "../5_Simulations/<executable_name>/data/", // up 1 in tree, "/5_Simulations/<executable_name>/" subdir - "../6_Advanced/<executable_name>/data/", // up 1 in tree, "/6_Advanced/<executable_name>/" subdir - "../7_CUDALibraries/<executable_name>/data/",// up 1 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../8_Android/<executable_name>/data/", // up 1 in tree, "/8_Android/<executable_name>/" subdir - "../samples/<executable_name>/data/", // up 1 in tree, "/samples/<executable_name>/" subdir - "../../", // up 2 in tree - "../../common/", // up 2 in tree, "/common/" subdir - "../../common/data/", // up 2 in tree, "/common/data/" subdir - "../../data/", // up 2 in tree, "/data/" subdir - "../../src/", // up 2 in tree, "/src/" subdir - "../../inc/", // up 2 in tree, "/inc/" subdir - "../../sandbox/<executable_name>/data/", // up 2 in tree, "/sandbox/<executable_name>/" subdir - "../../0_Simple/<executable_name>/data/", // up 2 in tree, "/0_Simple/<executable_name>/" subdir - "../../1_Utilities/<executable_name>/data/", // up 2 in tree, "/1_Utilities/<executable_name>/" subdir - "../../2_Graphics/<executable_name>/data/", // up 2 in tree, "/2_Graphics/<executable_name>/" subdir - "../../3_Imaging/<executable_name>/data/", // up 2 in tree, "/3_Imaging/<executable_name>/" subdir - "../../4_Finance/<executable_name>/data/", // up 2 in tree, "/4_Finance/<executable_name>/" subdir - "../../5_Simulations/<executable_name>/data/", // up 2 in tree, "/5_Simulations/<executable_name>/" subdir - "../../6_Advanced/<executable_name>/data/", // up 2 in tree, "/6_Advanced/<executable_name>/" subdir - "../../7_CUDALibraries/<executable_name>/data/", // up 2 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../8_Android/<executable_name>/data/", // up 2 in tree, "/8_Android/<executable_name>/" subdir - "../../samples/<executable_name>/data/", // up 2 in tree, "/samples/<executable_name>/" subdir - "../../../", // up 3 in tree - "../../../src/<executable_name>/", // up 3 in tree, "/src/<executable_name>/" subdir - "../../../src/<executable_name>/data/", // up 3 in tree, "/src/<executable_name>/data/" subdir - "../../../src/<executable_name>/src/", // up 3 in tree, "/src/<executable_name>/src/" subdir - "../../../src/<executable_name>/inc/", // up 3 in tree, "/src/<executable_name>/inc/" subdir - "../../../sandbox/<executable_name>/", // up 3 in tree, "/sandbox/<executable_name>/" subdir - "../../../sandbox/<executable_name>/data/", // up 3 in tree, "/sandbox/<executable_name>/data/" subdir - "../../../sandbox/<executable_name>/src/", // up 3 in tree, "/sandbox/<executable_name>/src/" subdir - "../../../sandbox/<executable_name>/inc/", // up 3 in tree, "/sandbox/<executable_name>/inc/" subdir - "../../../0_Simple/<executable_name>/data/", // up 3 in tree, "/0_Simple/<executable_name>/" subdir - "../../../1_Utilities/<executable_name>/data/", // up 3 in tree, "/1_Utilities/<executable_name>/" subdir - "../../../2_Graphics/<executable_name>/data/", // up 3 in tree, "/2_Graphics/<executable_name>/" subdir - "../../../3_Imaging/<executable_name>/data/", // up 3 in tree, "/3_Imaging/<executable_name>/" subdir - "../../../4_Finance/<executable_name>/data/", // up 3 in tree, "/4_Finance/<executable_name>/" subdir - "../../../5_Simulations/<executable_name>/data/", // up 3 in tree, "/5_Simulations/<executable_name>/" subdir - "../../../6_Advanced/<executable_name>/data/", // up 3 in tree, "/6_Advanced/<executable_name>/" subdir - "../../../7_CUDALibraries/<executable_name>/data/", // up 3 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../../8_Android/<executable_name>/data/", // up 3 in tree, "/8_Android/<executable_name>/" subdir - "../../../0_Simple/<executable_name>/", // up 3 in tree, "/0_Simple/<executable_name>/" subdir - "../../../1_Utilities/<executable_name>/", // up 3 in tree, "/1_Utilities/<executable_name>/" subdir - "../../../2_Graphics/<executable_name>/", // up 3 in tree, "/2_Graphics/<executable_name>/" subdir - "../../../3_Imaging/<executable_name>/", // up 3 in tree, "/3_Imaging/<executable_name>/" subdir - "../../../4_Finance/<executable_name>/", // up 3 in tree, "/4_Finance/<executable_name>/" subdir - "../../../5_Simulations/<executable_name>/", // up 3 in tree, "/5_Simulations/<executable_name>/" subdir - "../../../6_Advanced/<executable_name>/", // up 3 in tree, "/6_Advanced/<executable_name>/" subdir - "../../../7_CUDALibraries/<executable_name>/", // up 3 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../../8_Android/<executable_name>/", // up 3 in tree, "/8_Android/<executable_name>/" subdir - "../../../samples/<executable_name>/data/", // up 3 in tree, "/samples/<executable_name>/" subdir - "../../../common/", // up 3 in tree, "../../../common/" subdir - "../../../common/data/", // up 3 in tree, "../../../common/data/" subdir - "../../../data/", // up 3 in tree, "../../../data/" subdir - "../../../../", // up 4 in tree - "../../../../src/<executable_name>/", // up 4 in tree, "/src/<executable_name>/" subdir - "../../../../src/<executable_name>/data/", // up 4 in tree, "/src/<executable_name>/data/" subdir - "../../../../src/<executable_name>/src/", // up 4 in tree, "/src/<executable_name>/src/" subdir - "../../../../src/<executable_name>/inc/", // up 4 in tree, "/src/<executable_name>/inc/" subdir - "../../../../sandbox/<executable_name>/", // up 4 in tree, "/sandbox/<executable_name>/" subdir - "../../../../sandbox/<executable_name>/data/", // up 4 in tree, "/sandbox/<executable_name>/data/" subdir - "../../../../sandbox/<executable_name>/src/", // up 4 in tree, "/sandbox/<executable_name>/src/" subdir - "../../../../sandbox/<executable_name>/inc/", // up 4 in tree, "/sandbox/<executable_name>/inc/" subdir - "../../../../0_Simple/<executable_name>/data/", // up 4 in tree, "/0_Simple/<executable_name>/" subdir - "../../../../1_Utilities/<executable_name>/data/", // up 4 in tree, "/1_Utilities/<executable_name>/" subdir - "../../../../2_Graphics/<executable_name>/data/", // up 4 in tree, "/2_Graphics/<executable_name>/" subdir - "../../../../3_Imaging/<executable_name>/data/", // up 4 in tree, "/3_Imaging/<executable_name>/" subdir - "../../../../4_Finance/<executable_name>/data/", // up 4 in tree, "/4_Finance/<executable_name>/" subdir - "../../../../5_Simulations/<executable_name>/data/",// up 4 in tree, "/5_Simulations/<executable_name>/" subdir - "../../../../6_Advanced/<executable_name>/data/", // up 4 in tree, "/6_Advanced/<executable_name>/" subdir - "../../../../7_CUDALibraries/<executable_name>/data/", // up 4 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../../../8_Android/<executable_name>/data/", // up 4 in tree, "/8_Android/<executable_name>/" subdir - "../../../../0_Simple/<executable_name>/", // up 4 in tree, "/0_Simple/<executable_name>/" subdir - "../../../../1_Utilities/<executable_name>/", // up 4 in tree, "/1_Utilities/<executable_name>/" subdir - "../../../../2_Graphics/<executable_name>/", // up 4 in tree, "/2_Graphics/<executable_name>/" subdir - "../../../../3_Imaging/<executable_name>/", // up 4 in tree, "/3_Imaging/<executable_name>/" subdir - "../../../../4_Finance/<executable_name>/", // up 4 in tree, "/4_Finance/<executable_name>/" subdir - "../../../../5_Simulations/<executable_name>/",// up 4 in tree, "/5_Simulations/<executable_name>/" subdir - "../../../../6_Advanced/<executable_name>/", // up 4 in tree, "/6_Advanced/<executable_name>/" subdir - "../../../../7_CUDALibraries/<executable_name>/", // up 4 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../../../8_Android/<executable_name>/", // up 4 in tree, "/8_Android/<executable_name>/" subdir - "../../../../samples/<executable_name>/data/", // up 4 in tree, "/samples/<executable_name>/" subdir - "../../../../common/", // up 4 in tree, "../../../common/" subdir - "../../../../common/data/", // up 4 in tree, "../../../common/data/" subdir - "../../../../data/", // up 4 in tree, "../../../data/" subdir - "../../../../../", // up 5 in tree - "../../../../../src/<executable_name>/", // up 5 in tree, "/src/<executable_name>/" subdir - "../../../../../src/<executable_name>/data/", // up 5 in tree, "/src/<executable_name>/data/" subdir - "../../../../../src/<executable_name>/src/", // up 5 in tree, "/src/<executable_name>/src/" subdir - "../../../../../src/<executable_name>/inc/", // up 5 in tree, "/src/<executable_name>/inc/" subdir - "../../../../../sandbox/<executable_name>/", // up 5 in tree, "/sandbox/<executable_name>/" subdir - "../../../../../sandbox/<executable_name>/data/", // up 5 in tree, "/sandbox/<executable_name>/data/" subdir - "../../../../../sandbox/<executable_name>/src/", // up 5 in tree, "/sandbox/<executable_name>/src/" subdir - "../../../../../sandbox/<executable_name>/inc/", // up 5 in tree, "/sandbox/<executable_name>/inc/" subdir - "../../../../../0_Simple/<executable_name>/data/", // up 5 in tree, "/0_Simple/<executable_name>/" subdir - "../../../../../1_Utilities/<executable_name>/data/", // up 5 in tree, "/1_Utilities/<executable_name>/" subdir - "../../../../../2_Graphics/<executable_name>/data/", // up 5 in tree, "/2_Graphics/<executable_name>/" subdir - "../../../../../3_Imaging/<executable_name>/data/", // up 5 in tree, "/3_Imaging/<executable_name>/" subdir - "../../../../../4_Finance/<executable_name>/data/", // up 5 in tree, "/4_Finance/<executable_name>/" subdir - "../../../../../5_Simulations/<executable_name>/data/",// up 5 in tree, "/5_Simulations/<executable_name>/" subdir - "../../../../../6_Advanced/<executable_name>/data/", // up 5 in tree, "/6_Advanced/<executable_name>/" subdir - "../../../../../7_CUDALibraries/<executable_name>/data/", // up 5 in tree, "/7_CUDALibraries/<executable_name>/" subdir - "../../../../../8_Android/<executable_name>/data/", // up 5 in tree, "/8_Android/<executable_name>/" subdir - "../../../../../samples/<executable_name>/data/", // up 5 in tree, "/samples/<executable_name>/" subdir - "../../../../../common/", // up 5 in tree, "../../../common/" subdir - "../../../../../common/data/", // up 5 in tree, "../../../common/data/" subdir - }; - - // Extract the executable name - std::string executable_name; - - if (executable_path != 0) - { - executable_name = std::string(executable_path); - -#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) - // Windows path delimiter - size_t delimiter_pos = executable_name.find_last_of('\\'); - executable_name.erase(0, delimiter_pos + 1); - - if (executable_name.rfind(".exe") != std::string::npos) - { - // we strip .exe, only if the .exe is found - executable_name.resize(executable_name.size() - 4); - } - -#else - // Linux & OSX path delimiter - size_t delimiter_pos = executable_name.find_last_of('/'); - executable_name.erase(0,delimiter_pos+1); -#endif - } - - // Loop over all search paths and return the first hit - for (unsigned int i = 0; i < sizeof(searchPath)/sizeof(char *); ++i) - { - std::string path(searchPath[i]); - size_t executable_name_pos = path.find("<executable_name>"); - - // If there is executable_name variable in the searchPath - // replace it with the value - if (executable_name_pos != std::string::npos) - { - if (executable_path != 0) - { - path.replace(executable_name_pos, strlen("<executable_name>"), executable_name); - } - else - { - // Skip this path entry if no executable argument is given - continue; - } - } - -#ifdef _DEBUG - printf("sdkFindFilePath <%s> in %s\n", filename, path.c_str()); -#endif - - // Test if the file exists - path.append(filename); - FILE *fp; - FOPEN(fp, path.c_str(), "rb"); - - if (fp != NULL) - { - fclose(fp); - // File found - // returning an allocated array here for backwards compatibility reasons - char *file_path = (char *) malloc(path.length() + 1); - STRCPY(file_path, path.length() + 1, path.c_str()); - return file_path; - } - - if (fp) - { - fclose(fp); - } - } - - // File not found - return 0; -} #endif