diff --git a/doc.md b/doc.md index 1c30bd25..a838291b 100644 --- a/doc.md +++ b/doc.md @@ -370,14 +370,17 @@ cmake -B build-double-128 -DDATATYPE=DOUBLE -DBLOCKSIZE=128 ## To do list: - [X] fix issue of correctness between cpu and gpu in some benchmark -- [ ] Check for cl error dunring memory copy to host and clean +- [X] refactor to extract the common of frameworks +- [X] fix of the clock to be executed in runtime + kernerCLK->deviceOBJ +- [X] Check for cl error dunring memory copy to host and clean +- [ ] add UMA implementation for Android - [ ] Big cmake to compile everything +- [ ] be compatible with jetson board + add UMA for jetson board + - [ ] Tuto to install every deps(HIP,CUDA,OpenMP,OpenBLAS,CLBlast,FFTW3,OpenCL,CUDNN...) - [ ] Tuto how to use the benchmark(param etc) + compile with cmake -- [ ] fix of the clock to be executed in runtime -- [ ] add UMA implementation for Android -- [ ] be compatible with jetson board -- [ ] add UMA for jetson board -- [ ] create a test with vulkan for android to have best perfomance +- [ ] fix matrix_mult_FP16/Tensor/Memory_bandwith -> add timestamp csv format +**Bonus**: +- [ ] create a test with vulkan for android to have best perfomance (with softmax ?) diff --git a/gpu4s_benchmark/.~lock.results_1024.csv# b/gpu4s_benchmark/.~lock.results_1024.csv# deleted file mode 100644 index 39d3f7b3..00000000 --- a/gpu4s_benchmark/.~lock.results_1024.csv# +++ /dev/null @@ -1 +0,0 @@ -,noah,Fedora,29.07.2026 09:15,/home/noah/snap/onlyoffice-desktopeditors/1220/.local/share/onlyoffice; \ No newline at end of file diff --git a/gpu4s_benchmark/LRN_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/LRN_bench/cpu_functions/cpu_functions.h index 79ee5963..45599631 100644 --- a/gpu4s_benchmark/LRN_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/LRN_bench/cpu_functions/cpu_functions.h @@ -56,6 +56,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/LRN_bench/cuda/cuda_common.cu b/gpu4s_benchmark/LRN_bench/cuda/cuda_common.cu index dd563172..526927f7 100644 --- a/gpu4s_benchmark/LRN_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/LRN_bench/cuda/cuda_common.cu @@ -36,35 +36,28 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ -GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -72,50 +65,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -123,7 +125,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -133,9 +135,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -143,7 +144,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/LRN_bench/cuda/cuda_common.h b/gpu4s_benchmark/LRN_bench/cuda/cuda_common.h deleted file mode 100644 index 814541b0..00000000 --- a/gpu4s_benchmark/LRN_bench/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./LRN_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - diff --git a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda.cu index 901f6306..1ef938af 100644 --- a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda.cu @@ -29,18 +29,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + lrn_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_lib.cu index 90aa7618..556f9fb3 100644 --- a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_lib.cu @@ -21,18 +21,19 @@ #endif void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ - GraficObject* deviceObj = static_cast(device_object); - // CUDNN settings + GraficObject* deviceObj = static_cast(device_object); + // CUDNN settings const bench_t alf = 1; const bench_t bet = 0; cudnnHandle_t cudnn; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); checkCUDNN(cudnnCreate(&cudnn)); + // create input tensor cudnnTensorDescriptor_t input_descriptor; checkCUDNN(cudnnCreateTensorDescriptor(&input_descriptor)); @@ -73,17 +74,17 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, output_descriptor, deviceObj->d_B)); + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cuDNN cudnnDestroyTensorDescriptor(input_descriptor); cudnnDestroyTensorDescriptor(output_descriptor); cudnnDestroyLRNDescriptor(lrn_descriptor); - cudnnDestroy(cudnn); } diff --git a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_opt.cu index 86680d25..c5224b41 100644 --- a/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/LRN_bench/cuda/lib_cuda_opt.cu @@ -29,18 +29,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE); dim3 dimGrid(ceil(float((n*n))/(dimBlock.x))); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + lrn_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/LRN_bench/hip/hip_common.cpp b/gpu4s_benchmark/LRN_bench/hip/hip_common.cpp index 44f1467b..24f7a35b 100644 --- a/gpu4s_benchmark/LRN_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/LRN_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -36,36 +35,34 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -73,50 +70,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -124,7 +130,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -134,9 +140,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -144,7 +149,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/LRN_bench/hip/hip_common.h b/gpu4s_benchmark/LRN_bench/hip/hip_common.h deleted file mode 100644 index 4d8c4fdf..00000000 --- a/gpu4s_benchmark/LRN_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./LRN_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/LRN_bench/hip/lib_hip.cpp b/gpu4s_benchmark/LRN_bench/hip/lib_hip.cpp index f5e38788..2e3b08c6 100644 --- a/gpu4s_benchmark/LRN_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/LRN_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" /** @@ -28,17 +27,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL(lrn_kernel, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/LRN_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/LRN_bench/hip/lib_hip_opt.cpp index 25421a04..a5d8d763 100644 --- a/gpu4s_benchmark/LRN_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/LRN_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -30,17 +29,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE); dim3 dimGrid(ceil(float((n*n))/(dimBlock.x))); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL(relu_kernel, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/LRN_bench/main.cpp b/gpu4s_benchmark/LRN_bench/main.cpp index 32a34ca4..5d98ee2e 100644 --- a/gpu4s_benchmark/LRN_bench/main.cpp +++ b/gpu4s_benchmark/LRN_bench/main.cpp @@ -112,6 +112,14 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + lrn_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + lrn_bench->profiling_clock = true; + #endif + // init memory device_memory_init(lrn_bench, arguments_parameters->size * arguments_parameters->size, arguments_parameters->size * arguments_parameters->size); // copy memory to device @@ -224,6 +232,7 @@ void print_usage(const char * appName) printf(" -x: prints the timing of the validation. Only the sequential time of the application will be displayed\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -238,6 +247,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } @@ -266,6 +276,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par case 'i' : args +=1; strcpy(arguments_parameters->input_file_A,argv[args]); case 's' : args +=1; arguments_parameters->size = atol(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -276,4 +287,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par return ERROR_ARGUMENTS; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/LRN_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/LRN_bench/opencl/lib_opencl.cpp index 3c42edef..ab400952 100644 --- a/gpu4s_benchmark/LRN_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/LRN_bench/opencl/lib_opencl.cpp @@ -33,10 +33,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_add=cl::Kernel(program,"kernel_lrn"); kernel_add.setArg(0,*deviceObj->d_A); @@ -47,9 +48,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(5,BETA); deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/LRN_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/LRN_bench/opencl/lib_opencl_opt.cpp index 4370a22c..db7c8464 100644 --- a/gpu4s_benchmark/LRN_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/LRN_bench/opencl/lib_opencl_opt.cpp @@ -4,7 +4,6 @@ #include #include "GEN_kernel_opt.hcl" - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int w){ GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local = BLOCK_SIZE; @@ -35,9 +34,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_add=cl::Kernel(program,"kernel_lrn"); kernel_add.setArg(0,*deviceObj->d_A); @@ -48,9 +49,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(5,BETA); deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/LRN_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/LRN_bench/opencl/opencl_common.cpp index bfe73961..054070f2 100644 --- a/gpu4s_benchmark/LRN_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/LRN_bench/opencl/opencl_common.cpp @@ -59,11 +59,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -72,49 +72,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyB); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyB->wait(); - float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -123,7 +132,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/LRN_bench/opencl/opencl_common.h b/gpu4s_benchmark/LRN_bench/opencl/opencl_common.h deleted file mode 100644 index c3c52dca..00000000 --- a/gpu4s_benchmark/LRN_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./LRN_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/cifar_10/cpu_functions/cpu_functions.h b/gpu4s_benchmark/cifar_10/cpu_functions/cpu_functions.h index bb28f24d..7ffeda3d 100644 --- a/gpu4s_benchmark/cifar_10/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/cifar_10/cpu_functions/cpu_functions.h @@ -54,6 +54,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; diff --git a/gpu4s_benchmark/cifar_10/cuda/cuda_common.cu b/gpu4s_benchmark/cifar_10/cuda/cuda_common.cu index 4ba79109..ae0db0ff 100644 --- a/gpu4s_benchmark/cifar_10/cuda/cuda_common.cu +++ b/gpu4s_benchmark/cifar_10/cuda/cuda_common.cu @@ -6,8 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "cuda_common.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -140,12 +138,13 @@ bool device_memory_init(GraficCommon* device_object, unsigned int input_data, un void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->input_data, input_data, sizeof(bench_t) * input * input, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -176,51 +175,61 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben fprintf(stderr, "Failed to copy vector weights_layer_2 from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->output_data, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->output_data, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } //cudaMemcpy(h_C, deviceObj->dense_layer_2_output, 10 * sizeof(bench_t), cudaMemcpyDeviceToHost); + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -228,7 +237,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "GPU"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -238,10 +247,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - err = cudaFree(deviceObj->input_data); + cudaError_t err = cudaFree(deviceObj->input_data); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector input_data (error code %s)!\n", cudaGetErrorString(err)); @@ -249,19 +256,19 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->kernel_1); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector kernel_1 (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->conv_1_output); + err = cudaFree(deviceObj->conv_1_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector conv_1_output (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->pooling_1_output); if (err != cudaSuccess) { @@ -270,21 +277,20 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->kernel_2); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector kernel_2 (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->conv_2_output); + err = cudaFree(deviceObj->conv_2_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector conv_2_output (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->pooling_2_output); + err = cudaFree(deviceObj->pooling_2_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector pooling_2_output (error code %s)!\n", cudaGetErrorString(err)); @@ -292,19 +298,19 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->dense_layer_1_weights); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_1_weights (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->dense_layer_2_weights); + err = cudaFree(deviceObj->dense_layer_2_weights); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_weights (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->dense_layer_1_output); if (err != cudaSuccess) { @@ -313,21 +319,20 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->dense_layer_2_output); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_output (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->output_data); + err = cudaFree(deviceObj->output_data); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector output_data (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->sum_ouput); + err = cudaFree(deviceObj->sum_ouput); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector sum_ouput (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/cifar_10/cuda/cuda_common.h b/gpu4s_benchmark/cifar_10/cuda/cuda_common.h deleted file mode 100644 index 7f03860d..00000000 --- a/gpu4s_benchmark/cifar_10/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./cifar_10) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/cifar_10/cuda/lib_cuda.cu b/gpu4s_benchmark/cifar_10/cuda/lib_cuda.cu index 0efddf1c..b96e7f56 100644 --- a/gpu4s_benchmark/cifar_10/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/cifar_10/cuda/lib_cuda.cu @@ -8,7 +8,6 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size) { @@ -171,17 +170,18 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - cudaEventRecord(*deviceObj->start); dim3 dimBlock, dimGrid; dimBlock = dim3(BLOCK_SIZE, BLOCK_SIZE); dimGrid = dim3(ceil(float(input_data)/dimBlock.x), ceil(float(input_data)/dimBlock.y)); + + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + cudaEventRecord(*deviceObj->start); + + // 1-1 step convolution covolution_kernel<<>>(deviceObj->input_data, deviceObj->conv_1_output, deviceObj->kernel_1, input_data, input_data, input_data, kernel_1); // 1-2 step activation @@ -248,11 +248,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign softmax_kernel<<>>(deviceObj->dense_layer_2_output, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); softmax_finish_kernel<<>>(deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/cifar_10/cuda/lib_cuda_lib.cu index b70ff98b..aef9c2e3 100644 --- a/gpu4s_benchmark/cifar_10/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/cifar_10/cuda/lib_cuda_lib.cu @@ -695,10 +695,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign cudnnHandle_t cudnn; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); checkCUDNN(cudnnCreate(&cudnn)); @@ -734,12 +735,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign activation_d_2(device_object,cudnn, neurons_dense_2); //softmax softmax(device_object,cudnn, neurons_dense_2); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); cudnnDestroy(cudnn); } diff --git a/gpu4s_benchmark/cifar_10/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/cifar_10/cuda/lib_cuda_opt.cu index 07080595..708ba24f 100644 --- a/gpu4s_benchmark/cifar_10/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/cifar_10/cuda/lib_cuda_opt.cu @@ -6,7 +6,6 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 16 #define BLOCK_SIZE_PLANE (BLOCK_SIZE * BLOCK_SIZE) __global__ void @@ -334,25 +333,28 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - cudaEventRecord(*deviceObj->start); dim3 dimBlock, dimGrid,dimBlock_act, dimGrid_act; dimBlock = dim3(BLOCK_SIZE, BLOCK_SIZE); dimGrid = dim3(ceil(float(input_data)/dimBlock.x), ceil(float(input_data)/dimBlock.y)); unsigned int kernel_rad = kernel_1 / 2; unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); + + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + cudaEventRecord(*deviceObj->start); + + // 1-1 step convolution covolution_kernel<<>>(deviceObj->input_data, deviceObj->conv_1_output, deviceObj->kernel_1, input_data, input_data, input_data, kernel_1, size_shared_position, kernel_rad); + // 1-2 step activation dimBlock = dim3(BLOCK_SIZE_PLANE); dimGrid = dim3(ceil(float(input_data)/dimBlock.x)); relu_kernel<<>>(deviceObj->conv_1_output, deviceObj->conv_1_output, input_data*input_data); + // 1-3 step pooling unsigned int size_lateral_1 = input_data / stride_1; if(size_lateral_1*size_lateral_1 <= BLOCK_SIZE_PLANE) @@ -366,6 +368,7 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign dimGrid = dim3(ceil((size_lateral_1*size_lateral_1)/dimBlock.x)); } max_pooling_kernel<<>>(deviceObj->conv_1_output, deviceObj->pooling_1_output, input_data, stride_1, size_lateral_1); + // 1-4 normalization if(size_lateral_1 < BLOCK_SIZE) { @@ -431,10 +434,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign softmax_kernel<<>>(deviceObj->dense_layer_2_output, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); softmax_finish_kernel<<>>(deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10/hip/hip_common.cpp b/gpu4s_benchmark/cifar_10/hip/hip_common.cpp index a28f9ea1..fe9a99ec 100644 --- a/gpu4s_benchmark/cifar_10/hip/hip_common.cpp +++ b/gpu4s_benchmark/cifar_10/hip/hip_common.cpp @@ -36,115 +36,81 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ - GraficObject* deviceObj = static_cast(device_object); - // Allocate input - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->input_data, input_data * input_data * sizeof(bench_t)); - - if (err != hipSuccess) + GraficObject* deviceObj = static_cast(device_object); + + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + // Allocate input + hipError_t err = hipMalloc((void **)&deviceObj->input_data, input_data * input_data * sizeof(bench_t)); + if (err != hipSuccess) return false; + // Allocate kernel err = hipMalloc((void **)&deviceObj->kernel_1, kernel_1 * kernel_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate conv 1 output err = hipMalloc((void **)&deviceObj->conv_1_output, input_data * input_data * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate pooling output unsigned int size_pooling_1 = input_data / stride_1; err = hipMalloc((void **)&deviceObj->pooling_1_output, size_pooling_1 * size_pooling_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate kernel 2 err = hipMalloc((void **)&deviceObj->kernel_2, kernel_2 * kernel_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate conv 1 output err = hipMalloc((void **)&deviceObj->conv_2_output, size_pooling_1 * size_pooling_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate pooling output unsigned int size_pooling_2 = size_pooling_1 / stride_2; - err = hipMalloc((void **)&deviceObj->pooling_2_output, size_pooling_2 * size_pooling_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } //dense layer 1 weights unsigned int weights_layer_1 = size_pooling_2 * size_pooling_2 * neurons_dense_1; err = hipMalloc((void **)&deviceObj->dense_layer_1_weights, weights_layer_1* sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; + // dense layer output 1 err = hipMalloc((void **)&deviceObj->dense_layer_1_output, neurons_dense_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } //dense layer 2 weights unsigned int weights_layer_2 = neurons_dense_1 * neurons_dense_2; err = hipMalloc((void **)&deviceObj->dense_layer_2_weights, weights_layer_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // dense layer output 2 err = hipMalloc((void **)&deviceObj->dense_layer_2_output, neurons_dense_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // sum data err = hipMalloc((void **)&deviceObj->sum_ouput, sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // output data err = hipMalloc((void **)&deviceObj->output_data, neurons_dense_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->input_data, input_data, sizeof(bench_t) * input * input, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -175,12 +141,13 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben fprintf(stderr, "Failed to copy vector weights_layer_2 from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } @@ -189,41 +156,49 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->output_data, size * sizeof(bench_t), hipMemcpyDeviceToHost); + + hipError_t err = hipMemcpy(h_C, deviceObj->output_data, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } //hipMemcpy(h_C, deviceObj->dense_layer_2_output, 10 * sizeof(bench_t), hipMemcpyDeviceToHost); + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -231,7 +206,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -241,10 +216,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - err = hipFree(deviceObj->input_data); + hipError_t err = hipFree(deviceObj->input_data); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector input_data (error code %s)!\n", hipGetErrorString(err)); @@ -252,19 +225,19 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->kernel_1); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector kernel_1 (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->conv_1_output); + err = hipFree(deviceObj->conv_1_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector conv_1_output (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->pooling_1_output); if (err != hipSuccess) { @@ -273,21 +246,20 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->kernel_2); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector kernel_2 (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->conv_2_output); + err = hipFree(deviceObj->conv_2_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector conv_2_output (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->pooling_2_output); + err = hipFree(deviceObj->pooling_2_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector pooling_2_output (error code %s)!\n", hipGetErrorString(err)); @@ -295,19 +267,19 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->dense_layer_1_weights); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_1_weights (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->dense_layer_2_weights); + err = hipFree(deviceObj->dense_layer_2_weights); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_weights (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->dense_layer_1_output); if (err != hipSuccess) { @@ -316,21 +288,20 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->dense_layer_2_output); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_output (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->output_data); + err = hipFree(deviceObj->output_data); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector output_data (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->sum_ouput); + err = hipFree(deviceObj->sum_ouput); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector sum_ouput (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/cifar_10/hip/hip_common.h b/gpu4s_benchmark/cifar_10/hip/hip_common.h deleted file mode 100644 index 0cf309bd..00000000 --- a/gpu4s_benchmark/cifar_10/hip/hip_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./cifar_10) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/cifar_10/hip/lib_hip.cpp b/gpu4s_benchmark/cifar_10/hip/lib_hip.cpp index e05a9cc6..f7998347 100644 --- a/gpu4s_benchmark/cifar_10/hip/lib_hip.cpp +++ b/gpu4s_benchmark/cifar_10/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" /** @@ -7,8 +6,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size) { unsigned int size = n; @@ -171,17 +170,17 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - (void)hipEventRecord(*deviceObj->start); dim3 dimBlock, dimGrid; dimBlock = dim3(BLOCK_SIZE, BLOCK_SIZE); dimGrid = dim3(ceil(float(input_data)/dimBlock.x), ceil(float(input_data)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + (void)hipEventRecord(*deviceObj->start); + + // 1-1 step convolution hipLaunchKernelGGL((covolution_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->input_data, deviceObj->conv_1_output, deviceObj->kernel_1, input_data, input_data, input_data, kernel_1); // 1-2 step activation @@ -248,10 +247,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign hipLaunchKernelGGL((softmax_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->dense_layer_2_output, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); hipLaunchKernelGGL((softmax_finish_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10/hip/lib_hip_opt.cpp b/gpu4s_benchmark/cifar_10/hip/lib_hip_opt.cpp index 9d52c3fa..0825ed44 100644 --- a/gpu4s_benchmark/cifar_10/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/cifar_10/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -8,7 +7,6 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 16 #define BLOCK_SIZE_PLANE (BLOCK_SIZE * BLOCK_SIZE) __global__ void @@ -334,20 +332,20 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - (void)hipEventRecord(*deviceObj->start); dim3 dimBlock, dimGrid,dimBlock_act, dimGrid_act; dimBlock = dim3(BLOCK_SIZE, BLOCK_SIZE); dimGrid = dim3(ceil(float(input_data)/dimBlock.x), ceil(float(input_data)/dimBlock.y)); unsigned int kernel_rad = kernel_1 / 2; unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + (void)hipEventRecord(*deviceObj->start); + + // 1-1 step convolution hipLaunchKernelGGL((covolution_kernel), dim3(dimGrid), dim3(dimBlock), size_shared, 0, deviceObj->input_data, deviceObj->conv_1_output, deviceObj->kernel_1, input_data, input_data, input_data, kernel_1, size_shared_position, kernel_rad); // 1-2 step activation dimBlock = dim3(BLOCK_SIZE_PLANE); @@ -431,10 +429,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign hipLaunchKernelGGL((softmax_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->dense_layer_2_output, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); hipLaunchKernelGGL((softmax_finish_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->output_data, deviceObj->sum_ouput, neurons_dense_2); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10/main.cpp b/gpu4s_benchmark/cifar_10/main.cpp index 8226e7cb..1821c64e 100644 --- a/gpu4s_benchmark/cifar_10/main.cpp +++ b/gpu4s_benchmark/cifar_10/main.cpp @@ -180,7 +180,15 @@ int main(int argc, char *argv[]){ if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } - + + // Update profiling clock mode + cifar10_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + cifar10_bench->profiling_clock = true; + #endif + // init memory bool mem_result = true; mem_result = device_memory_init(cifar10_bench, CIFAR_10_INPUT, CIFAR_10_OUTPUT, KERNEL_CON_1, KERNEL_CON_2, STRIDE_1, STRIDE_2, DENSE_1, DENSE_2); @@ -302,6 +310,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -315,6 +324,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -336,6 +346,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par args +=1; strcpy(arguments_parameters->input_file_B,argv[args]); break; + case 'p' : arguments_parameters->profiling_clock = true;break; // specific case 'i' : args +=1; strcpy(arguments_parameters->input_file_A,argv[args]); @@ -354,4 +365,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par bench_t RandomNumber() { return ((bench_t(rand()) / bench_t(RAND_MAX)) * (MAX_VALUE - MIN_VALUE)) + MIN_VALUE; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/cifar_10/opencl/lib_opencl.cpp b/gpu4s_benchmark/cifar_10/opencl/lib_opencl.cpp index 925ca406..e0962dda 100644 --- a/gpu4s_benchmark/cifar_10/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/cifar_10/opencl/lib_opencl.cpp @@ -25,10 +25,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); // 1-1 step convolution if (input_data <= BLOCK_SIZE) { @@ -250,9 +251,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign deviceObj->queue->enqueueNDRangeKernel(softmax_end_kernel,cl::NullRange,global,local, NULL, deviceObj->evt_softmax_fin); // end + // Wait for completion before stopping the clock deviceObj->queue->finish(); - - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/cifar_10/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/cifar_10/opencl/lib_opencl_opt.cpp index 783aa744..586800b1 100644 --- a/gpu4s_benchmark/cifar_10/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/cifar_10/opencl/lib_opencl_opt.cpp @@ -4,7 +4,6 @@ #include "GEN_kernel_opt.hcl" #include "GEN_atomic_functions.hcl" - #define BLOCK_SIZE_PLANE BLOCK_SIZE*BLOCK_SIZE void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2){ @@ -43,10 +42,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); // 1-1 step convolution cl::Kernel kernel_conv=cl::Kernel(program,"kernel_matrix_convolution"); @@ -304,10 +304,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign deviceObj->queue->enqueueNDRangeKernel(softmax_end_kernel,cl::NullRange,global,local, NULL, deviceObj->evt_softmax_fin); // end + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/cifar_10/opencl/opencl_common.cpp b/gpu4s_benchmark/cifar_10/opencl/opencl_common.cpp index 50f5eb82..a869945d 100644 --- a/gpu4s_benchmark/cifar_10/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/cifar_10/opencl/opencl_common.cpp @@ -122,11 +122,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int input_data, un void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); // input data cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->input_data,CL_TRUE,0,sizeof(bench_t)* input * input, input_data, NULL, deviceObj->evt_copyIN); @@ -166,27 +166,36 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->output_data,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyOut); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->output_data, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyOut); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (OpenCL error code %d)!\n", err); + return; + } //deviceObj->queue->enqueueReadBuffer(*deviceObj->conv_2_output,CL_TRUE,0,sizeof(bench_t)*16*16,h_C, NULL, deviceObj->evt_copyOut); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ @@ -194,42 +203,41 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for deviceObj->evt_copyOut->wait(); float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - - // copy memory H -> D - elapsed_h_d = deviceObj->evt_copyIN->getProfilingInfo() - deviceObj->evt_copyIN->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyK1->getProfilingInfo() - deviceObj->evt_copyK1->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyK2->getProfilingInfo() - deviceObj->evt_copyK2->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyW1->getProfilingInfo() - deviceObj->evt_copyW1->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyW2->getProfilingInfo() - deviceObj->evt_copyW2->getProfilingInfo(); - - // kernel time - elapsed = deviceObj->evt1_1->getProfilingInfo() - deviceObj->evt1_1->getProfilingInfo(); - elapsed += deviceObj->evt1_2->getProfilingInfo() - deviceObj->evt1_2->getProfilingInfo(); - elapsed += deviceObj->evt1_3->getProfilingInfo() - deviceObj->evt1_3->getProfilingInfo(); - elapsed += deviceObj->evt1_4->getProfilingInfo() - deviceObj->evt1_4->getProfilingInfo(); - elapsed += deviceObj->evt2_1->getProfilingInfo() - deviceObj->evt2_1->getProfilingInfo(); - elapsed += deviceObj->evt2_2->getProfilingInfo() - deviceObj->evt2_2->getProfilingInfo(); - elapsed += deviceObj->evt2_3->getProfilingInfo() - deviceObj->evt2_3->getProfilingInfo(); - elapsed += deviceObj->evt2_4->getProfilingInfo() - deviceObj->evt2_4->getProfilingInfo(); - elapsed += deviceObj->evtd_1->getProfilingInfo() - deviceObj->evtd_1->getProfilingInfo(); - elapsed += deviceObj->evtd_1_a->getProfilingInfo() - deviceObj->evtd_1_a->getProfilingInfo(); - elapsed += deviceObj->evtd_2->getProfilingInfo() - deviceObj->evtd_2->getProfilingInfo(); - elapsed += deviceObj->evtd_2_a->getProfilingInfo() - deviceObj->evtd_2_a->getProfilingInfo(); - elapsed += deviceObj->evt_softmax->getProfilingInfo() - deviceObj->evt_softmax->getProfilingInfo(); - elapsed += deviceObj->evt_softmax_fin->getProfilingInfo() - deviceObj->evt_softmax_fin->getProfilingInfo(); - - // copy memory D -> H - elapsed_d_h = deviceObj->evt_copyOut->getProfilingInfo() - deviceObj->evt_copyOut->getProfilingInfo(); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + + // copy memory H -> D + elapsed_h_d = deviceObj->evt_copyIN->getProfilingInfo() - deviceObj->evt_copyIN->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyK1->getProfilingInfo() - deviceObj->evt_copyK1->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyK2->getProfilingInfo() - deviceObj->evt_copyK2->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyW1->getProfilingInfo() - deviceObj->evt_copyW1->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyW2->getProfilingInfo() - deviceObj->evt_copyW2->getProfilingInfo(); + + // kernel time + elapsed = deviceObj->evt1_1->getProfilingInfo() - deviceObj->evt1_1->getProfilingInfo(); + elapsed += deviceObj->evt1_2->getProfilingInfo() - deviceObj->evt1_2->getProfilingInfo(); + elapsed += deviceObj->evt1_3->getProfilingInfo() - deviceObj->evt1_3->getProfilingInfo(); + elapsed += deviceObj->evt1_4->getProfilingInfo() - deviceObj->evt1_4->getProfilingInfo(); + elapsed += deviceObj->evt2_1->getProfilingInfo() - deviceObj->evt2_1->getProfilingInfo(); + elapsed += deviceObj->evt2_2->getProfilingInfo() - deviceObj->evt2_2->getProfilingInfo(); + elapsed += deviceObj->evt2_3->getProfilingInfo() - deviceObj->evt2_3->getProfilingInfo(); + elapsed += deviceObj->evt2_4->getProfilingInfo() - deviceObj->evt2_4->getProfilingInfo(); + elapsed += deviceObj->evtd_1->getProfilingInfo() - deviceObj->evtd_1->getProfilingInfo(); + elapsed += deviceObj->evtd_1_a->getProfilingInfo() - deviceObj->evtd_1_a->getProfilingInfo(); + elapsed += deviceObj->evtd_2->getProfilingInfo() - deviceObj->evtd_2->getProfilingInfo(); + elapsed += deviceObj->evtd_2_a->getProfilingInfo() - deviceObj->evtd_2_a->getProfilingInfo(); + elapsed += deviceObj->evt_softmax->getProfilingInfo() - deviceObj->evt_softmax->getProfilingInfo(); + elapsed += deviceObj->evt_softmax_fin->getProfilingInfo() - deviceObj->evt_softmax_fin->getProfilingInfo(); + + // copy memory D -> H + elapsed_d_h = deviceObj->evt_copyOut->getProfilingInfo() - deviceObj->evt_copyOut->getProfilingInfo(); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0, current_time); @@ -237,7 +245,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "GPU"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/cifar_10/opencl/opencl_common.h b/gpu4s_benchmark/cifar_10/opencl/opencl_common.h deleted file mode 100644 index 59c3cbcb..00000000 --- a/gpu4s_benchmark/cifar_10/opencl/opencl_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./cifar_10) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/cifar_10_multiple/cpu_functions/cpu_functions.h b/gpu4s_benchmark/cifar_10_multiple/cpu_functions/cpu_functions.h index e3725bf0..d50e3835 100644 --- a/gpu4s_benchmark/cifar_10_multiple/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/cifar_10_multiple/cpu_functions/cpu_functions.h @@ -55,6 +55,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; diff --git a/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.cu b/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.cu index 76edea16..ca1df213 100644 --- a/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.cu +++ b/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.cu @@ -6,9 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "cuda_common.h" - - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -38,115 +35,77 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ - GraficObject* deviceObj = static_cast(device_object); - // Allocate input - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&(deviceObj->input_data), number_of_images * input_data * input_data * sizeof(bench_t)); + GraficObject* deviceObj = static_cast(device_object); + // Allocate input - if (err != cudaSuccess) - { - return false; - } + cudaError_t err = cudaMalloc((void **)&(deviceObj->input_data), number_of_images * input_data * input_data * sizeof(bench_t)); + if (err != cudaSuccess) return false; + // Allocate kernel err = cudaMalloc((void **)&(deviceObj->kernel_1), kernel_1 * kernel_1 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // Allocate conv 1 output err = cudaMalloc((void **)&(deviceObj->conv_1_output), input_data * input_data * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // Allocate pooling output unsigned int size_pooling_1 = input_data / stride_1; err = cudaMalloc((void **)&(deviceObj->pooling_1_output), size_pooling_1 * size_pooling_1 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // Allocate kernel 2 err = cudaMalloc((void **)&(deviceObj->kernel_2), kernel_2 * kernel_2 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // Allocate conv 1 output err = cudaMalloc((void **)&(deviceObj->conv_2_output), size_pooling_1 * size_pooling_1 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // Allocate pooling output unsigned int size_pooling_2 = size_pooling_1 / stride_2; err = cudaMalloc((void **)&(deviceObj->pooling_2_output), size_pooling_2 * size_pooling_2 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + //dense layer 1 weights unsigned int weights_layer_1 = size_pooling_2 * size_pooling_2 * neurons_dense_1; - err = cudaMalloc((void **)&(deviceObj->dense_layer_1_weights), weights_layer_1* sizeof(bench_t)); + if (err != cudaSuccess) return false; + - if (err != cudaSuccess) - { - return false; - } // dense layer output 1 err = cudaMalloc((void **)&(deviceObj->dense_layer_1_output), neurons_dense_1 * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } //dense layer 2 weights unsigned int weights_layer_2 = neurons_dense_1 * neurons_dense_2; err = cudaMalloc((void **)&(deviceObj->dense_layer_2_weights), weights_layer_2 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // dense layer output 2 err = cudaMalloc((void **)&(deviceObj->dense_layer_2_output), neurons_dense_2 * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // sum data err = cudaMalloc((void **)&(deviceObj->sum_ouput), sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + // output data err = cudaMalloc((void **)&(deviceObj->output_data), number_of_images * neurons_dense_2 * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->input_data, input_data, sizeof(bench_t) * input * input * number_of_images, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -178,53 +137,60 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben return; } cudaMemset(deviceObj->sum_ouput, 0, sizeof(bench_t)); - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } - void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->output_data, number_of_images * size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->output_data, number_of_images * size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } //cudaMemcpy(h_C, deviceObj->dense_layer_2_output, 10 * sizeof(bench_t), cudaMemcpyDeviceToHost); + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time) { GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); //wait float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -232,7 +198,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -242,10 +208,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - err = cudaFree(deviceObj->input_data); + cudaError_t err = cudaFree(deviceObj->input_data); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector input_data (error code %s)!\n", cudaGetErrorString(err)); @@ -253,19 +217,19 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->kernel_1); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector kernel_1 (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->conv_1_output); + err = cudaFree(deviceObj->conv_1_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector conv_1_output (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->pooling_1_output); if (err != cudaSuccess) { @@ -274,21 +238,20 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->kernel_2); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector kernel_2 (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->conv_2_output); + err = cudaFree(deviceObj->conv_2_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector conv_2_output (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->pooling_2_output); + err = cudaFree(deviceObj->pooling_2_output); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector pooling_2_output (error code %s)!\n", cudaGetErrorString(err)); @@ -296,19 +259,19 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->dense_layer_1_weights); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_1_weights (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->dense_layer_2_weights); + err = cudaFree(deviceObj->dense_layer_2_weights); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_weights (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->dense_layer_1_output); if (err != cudaSuccess) { @@ -317,21 +280,20 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->dense_layer_2_output); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_output (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->output_data); + err = cudaFree(deviceObj->output_data); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector output_data (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->sum_ouput); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector sum_ouput (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.h b/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.h deleted file mode 100644 index 6bd66a8e..00000000 --- a/gpu4s_benchmark/cifar_10_multiple/cuda/cuda_common.h +++ /dev/null @@ -1,19 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./cifar_10_multiple) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - diff --git a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda.cu b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda.cu index 6932fc79..08efb675 100644 --- a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda.cu @@ -7,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size) { unsigned int size = n; @@ -173,16 +173,15 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - // execute net - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; - cudaEventRecord(*deviceObj->start); bench_t* aux_output_data = deviceObj->output_data; bench_t* aux_input_data = deviceObj->input_data; + + // profilling start + kernelCLK.start(); + cudaEventRecord(*deviceObj->start); for(unsigned int position = 0; position < number_of_images; ++position) { @@ -260,11 +259,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign softmax_finish_kernel<<>>(aux_output_data, deviceObj->sum_ouput, neurons_dense_2); cudaMemset(deviceObj->sum_ouput, 0, sizeof(bench_t)); } + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_lib.cu index f741824e..f482699b 100644 --- a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_lib.cu @@ -705,20 +705,20 @@ void clean_cudnn(){ /////////////////////////////////////////////////////////////////////////////////// void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ - GraficObject* deviceObj = static_cast(device_object); - // cublas settings - + GraficObject* deviceObj = static_cast(device_object); + // cublas settings cudnnHandle_t cudnn; + checkCUDNN(cudnnCreate(&cudnn)); + // init structures + cuddObject *cudd_object = (cuddObject *)malloc(sizeof(cuddObject)); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); - checkCUDNN(cudnnCreate(&cudnn)); - // init structures - cuddObject *cudd_object = (cuddObject *)malloc(sizeof(cuddObject)); // init comvolution convolution_1_1_init(cudd_object, input_data, kernel_1); for(unsigned int position = 0; position < number_of_images; ++position) @@ -757,16 +757,18 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign softmax(device_object,cudnn, neurons_dense_2, position * output_data); } + + // profilling end + cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); + convolution_1_1_clear(cudd_object); clean_cudnn(); // delete struct free(cudd_object); cudnnDestroy(cudnn); - cudaEventRecord(*deviceObj->stop); - - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif - } diff --git a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_opt.cu index 87970232..fcc5f627 100644 --- a/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/cifar_10_multiple/cuda/lib_cuda_opt.cu @@ -6,8 +6,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -#define BLOCK_SIZE_PLANE (BLOCK_SIZE * BLOCK_SIZE) + + #define BLOCK_SIZE_PLANE (BLOCK_SIZE * BLOCK_SIZE) #ifndef NUMBER_OF_STREAMS #define NUMBER_OF_STREAMS 8 #endif @@ -338,14 +338,9 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; - cudaEventRecord(*deviceObj->start); bench_t* aux_output_data; bench_t* aux_input_data; bench_t* aux_convolution_1_output; @@ -358,14 +353,19 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign unsigned int size_lateral_1 = input_data / stride_1; unsigned int size_lateral_2 = size_lateral_1 / stride_2; + // create streams cudaStream_t cuda_streams[NUMBER_OF_STREAMS]; for (unsigned int streams = 0; streams < NUMBER_OF_STREAMS; ++streams) { cudaStreamCreate(&cuda_streams[streams]); } - + // profilling start + kernelCLK.start(); + cudaEventRecord(*deviceObj->start); + + // 1-1 step convolution for(unsigned int position = 0; position < number_of_images; ++position) { @@ -485,10 +485,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign cudaMemsetAsync(aux_sum, 0, sizeof(bench_t),cuda_streams[stream]); } + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.cpp b/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.cpp index 609bf178..b804083e 100644 --- a/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.cpp @@ -6,9 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" - - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -38,115 +35,83 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ - GraficObject* deviceObj = static_cast(device_object); - // Allocate input - hipError_t err = hipSuccess; - err = hipMalloc((void **)&(deviceObj->input_data), number_of_images * input_data * input_data * sizeof(bench_t)); + GraficObject* deviceObj = static_cast(device_object); - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate input + hipError_t err = hipMalloc((void **)&(deviceObj->input_data), number_of_images * input_data * input_data * sizeof(bench_t)); + if (err != hipSuccess) return false; + // Allocate kernel err = hipMalloc((void **)&(deviceObj->kernel_1), kernel_1 * kernel_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate conv 1 output err = hipMalloc((void **)&(deviceObj->conv_1_output), input_data * input_data * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate pooling output unsigned int size_pooling_1 = input_data / stride_1; err = hipMalloc((void **)&(deviceObj->pooling_1_output), size_pooling_1 * size_pooling_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate kernel 2 err = hipMalloc((void **)&(deviceObj->kernel_2), kernel_2 * kernel_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate conv 1 output err = hipMalloc((void **)&(deviceObj->conv_2_output), size_pooling_1 * size_pooling_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate pooling output unsigned int size_pooling_2 = size_pooling_1 / stride_2; err = hipMalloc((void **)&(deviceObj->pooling_2_output), size_pooling_2 * size_pooling_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } //dense layer 1 weights unsigned int weights_layer_1 = size_pooling_2 * size_pooling_2 * neurons_dense_1; err = hipMalloc((void **)&(deviceObj->dense_layer_1_weights), weights_layer_1* sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // dense layer output 1 err = hipMalloc((void **)&(deviceObj->dense_layer_1_output), neurons_dense_1 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } //dense layer 2 weights unsigned int weights_layer_2 = neurons_dense_1 * neurons_dense_2; err = hipMalloc((void **)&(deviceObj->dense_layer_2_weights), weights_layer_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // dense layer output 2 err = hipMalloc((void **)&(deviceObj->dense_layer_2_output), neurons_dense_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // sum data err = hipMalloc((void **)&(deviceObj->sum_ouput), sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // output data err = hipMalloc((void **)&(deviceObj->output_data), number_of_images * neurons_dense_2 * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->input_data, input_data, sizeof(bench_t) * input * input * number_of_images, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -178,54 +143,63 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben return; } hipMemset(deviceObj->sum_ouput, 0, sizeof(bench_t)); + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->output_data, number_of_images * size * sizeof(bench_t), hipMemcpyDeviceToHost); + + hipError_t err = hipMemcpy(h_C, deviceObj->output_data, number_of_images * size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } //hipMemcpy(h_C, deviceObj->dense_layer_2_output, 10 * sizeof(bench_t), hipMemcpyDeviceToHost); + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time) { GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -233,7 +207,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -243,10 +217,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - err = hipFree(deviceObj->input_data); + hipError_t err = hipFree(deviceObj->input_data); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector input_data (error code %s)!\n", hipGetErrorString(err)); @@ -254,19 +226,19 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->kernel_1); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector kernel_1 (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->conv_1_output); + err = hipFree(deviceObj->conv_1_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector conv_1_output (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->pooling_1_output); if (err != hipSuccess) { @@ -275,21 +247,20 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->kernel_2); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector kernel_2 (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->conv_2_output); + err = hipFree(deviceObj->conv_2_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector conv_2_output (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->pooling_2_output); + err = hipFree(deviceObj->pooling_2_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector pooling_2_output (error code %s)!\n", hipGetErrorString(err)); @@ -297,26 +268,25 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->dense_layer_1_weights); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_1_weights (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->dense_layer_2_weights); + err = hipFree(deviceObj->dense_layer_2_weights); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_2_weights (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->dense_layer_1_output); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector dense_layer_1_output (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->dense_layer_2_output); if (err != hipSuccess) @@ -324,15 +294,15 @@ void clean(GraficCommon* device_object){ fprintf(stderr, "Failed to free device vector dense_layer_2_output (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->output_data); + err = hipFree(deviceObj->output_data); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector output_data (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->sum_ouput); + err = hipFree(deviceObj->sum_ouput); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector sum_ouput (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.h b/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.h deleted file mode 100644 index 6eff58aa..00000000 --- a/gpu4s_benchmark/cifar_10_multiple/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./cifar_10_multiple) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip.cpp b/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip.cpp index 7daed300..b753fa17 100644 --- a/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -173,16 +172,14 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - // execute net - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - (void)hipEventRecord(*deviceObj->start); bench_t* aux_output_data = deviceObj->output_data; bench_t* aux_input_data = deviceObj->input_data; + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + (void)hipEventRecord(*deviceObj->start); for(unsigned int position = 0; position < number_of_images; ++position) { @@ -260,11 +257,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign hipLaunchKernelGGL((softmax_finish_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, aux_output_data, deviceObj->sum_ouput, neurons_dense_2); hipMemset(deviceObj->sum_ouput, 0, sizeof(bench_t)); } + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip_opt.cpp b/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip_opt.cpp index f102a1cf..bfe79ee5 100644 --- a/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" /** @@ -298,14 +297,6 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - // execute net - // 1-1 step convolution - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - - (void)hipEventRecord(*deviceObj->start); bench_t* aux_output_data; bench_t* aux_input_data; bench_t* aux_convolution_1_output; @@ -323,9 +314,17 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign for (unsigned int streams = 0; streams < NUMBER_OF_STREAMS; ++streams) { (void)hipStreamCreate(&cuda_streams[streams]); - } - + } + // kernel time execution + Clock kernelCLK; + + // profilling start + kernelCLK.start(); + (void)hipEventRecord(*deviceObj->start); + + + // 1-1 step convolution for(unsigned int position = 0; position < number_of_images; ++position) { @@ -447,8 +446,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign } (void)hipEventRecord(*deviceObj->stop); - #ifdef PROFILING_CLOCK - (void)hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // profilling end + (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/main.cpp b/gpu4s_benchmark/cifar_10_multiple/main.cpp index 675ccf94..c909d03d 100644 --- a/gpu4s_benchmark/cifar_10_multiple/main.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/main.cpp @@ -193,6 +193,15 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + cifar10_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + cifar10_bench->profiling_clock = true; + #endif + + // init memory bool mem_result = true; mem_result = device_memory_init(cifar10_bench, CIFAR_10_INPUT, CIFAR_10_OUTPUT, KERNEL_CON_1, KERNEL_CON_2, STRIDE_1, STRIDE_2, DENSE_1, DENSE_2, arguments_parameters->size); @@ -326,6 +335,7 @@ void print_usage(const char * appName) printf(" -x: prints the timing of the validation. Only the sequential time of the application will be displayed\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -340,6 +350,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -373,6 +384,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -390,4 +402,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par bench_t RandomNumber() { return ((bench_t(rand()) / bench_t(RAND_MAX)) * (MAX_VALUE - MIN_VALUE)) + MIN_VALUE; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl.cpp b/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl.cpp index aa27a276..e9809c60 100644 --- a/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl.cpp @@ -4,7 +4,6 @@ #include "GEN_kernel.hcl" #include "GEN_atomic_functions.hcl" - void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); unsigned int x_local= BLOCK_SIZE; @@ -25,11 +24,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign // timing deviceObj->queue->finish(); // Clear queue to ensure accurate start - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt1_1); @@ -260,10 +259,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_softmax_fin); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); - - #ifdef PROFILING_CLOCK + // Clock profilling end kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl_opt.cpp index 60972f65..593f498a 100644 --- a/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/opencl/lib_opencl_opt.cpp @@ -39,9 +39,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign } // timing deviceObj->queue->finish(); // Clear queue to ensure accurate start - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); //FIX : GPU profiling use opencl marker @@ -351,12 +353,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int input_data, unsign //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_softmax_fin); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.cpp b/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.cpp index 5aa57938..2d2bb815 100644 --- a/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.cpp @@ -8,7 +8,6 @@ #include "../benchmark_library.h" #include - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -63,8 +62,6 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na deviceObj->evtd_2_a = new cl::Event; deviceObj->evt_softmax = new cl::Event; deviceObj->evt_softmax_fin = new cl::Event; - - } bool device_memory_init(GraficCommon* device_object, unsigned int input_data, unsigned int output_data, unsigned int kernel_1, unsigned int kernel_2, unsigned int stride_1, unsigned int stride_2, unsigned int neurons_dense_1, unsigned int neurons_dense_2, unsigned int number_of_images){ @@ -132,12 +129,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int input_data, un void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, bench_t* kernel_1_data, bench_t* kernel_2_data, bench_t* weights_1 ,bench_t* weights_2,unsigned int input , unsigned int kernel_size_1, unsigned int kernel_size_2, unsigned int weights_1_size, unsigned int weights_2_size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + // Clock profilling start + h2dCLK.start(); // input data cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->input_data,CL_TRUE,0,sizeof(bench_t)* input * input * number_of_images, input_data, NULL, deviceObj->evt_copyIN); @@ -177,10 +173,11 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } @@ -188,17 +185,25 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* input_data, ben void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size, unsigned int number_of_images){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; - deviceObj->queue->enqueueReadBuffer(*deviceObj->output_data,CL_TRUE,0,sizeof(bench_t)*size*number_of_images,h_C, NULL, deviceObj->evt_copyOut); + // Clock profilling start + d2hCLK.start(); + + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->output_data, CL_TRUE, 0, sizeof(bench_t)*size*number_of_images, h_C, NULL, deviceObj->evt_copyOut); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector output_data from device to host (OpenCL error code %d)!\n", err); + return; + } //deviceObj->queue->enqueueReadBuffer(*deviceObj->conv_2_output,CL_TRUE,0,sizeof(bench_t)*16*16,h_C, NULL, deviceObj->evt_copyOut); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time) @@ -207,29 +212,28 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for deviceObj->evt_copyOut->wait(); float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - - // copy memory H -> D - elapsed_h_d = deviceObj->evt_copyIN->getProfilingInfo() - deviceObj->evt_copyIN->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyK1->getProfilingInfo() - deviceObj->evt_copyK1->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyK2->getProfilingInfo() - deviceObj->evt_copyK2->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyW1->getProfilingInfo() - deviceObj->evt_copyW1->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyW2->getProfilingInfo() - deviceObj->evt_copyW2->getProfilingInfo(); - - // kernel time - elapsed = deviceObj->evt_softmax_fin->getProfilingInfo() - deviceObj->evt1_1->getProfilingInfo(); - // copy memory D -> H - elapsed_d_h = deviceObj->evt_copyOut->getProfilingInfo() - deviceObj->evt_copyOut->getProfilingInfo(); - - #ifdef PROFILING_CLOCK + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + + // copy memory H -> D + elapsed_h_d = deviceObj->evt_copyIN->getProfilingInfo() - deviceObj->evt_copyIN->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyK1->getProfilingInfo() - deviceObj->evt_copyK1->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyK2->getProfilingInfo() - deviceObj->evt_copyK2->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyW1->getProfilingInfo() - deviceObj->evt_copyW1->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyW2->getProfilingInfo() - deviceObj->evt_copyW2->getProfilingInfo(); + + // kernel time + elapsed = deviceObj->evt_softmax_fin->getProfilingInfo() - deviceObj->evt1_1->getProfilingInfo(); + + // copy memory D -> H + elapsed_d_h = deviceObj->evt_copyOut->getProfilingInfo() - deviceObj->evt_copyOut->getProfilingInfo(); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,deviceObj->elapsed_time,elapsed_d_h / 1000000.0, current_time); @@ -237,7 +241,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,deviceObj->elapsed_time,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); @@ -246,8 +250,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for } void clean(GraficCommon* device_object){ - -GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); // pointers clean delete deviceObj->context; diff --git a/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.h b/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.h deleted file mode 100644 index a9ea59be..00000000 --- a/gpu4s_benchmark/cifar_10_multiple/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./cifar_10_multiple) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/common/benchmark_common.h b/gpu4s_benchmark/common/benchmark_common.h index 72e78025..3e1bd6c3 100644 --- a/gpu4s_benchmark/common/benchmark_common.h +++ b/gpu4s_benchmark/common/benchmark_common.h @@ -27,6 +27,20 @@ #elif HIP // HIP part #include + + inline void hipDumbSync() + { + void* dumb_ptr; + hipError_t err = hipMalloc(&dumb_ptr, 4); + err = hipMemset(dumb_ptr, 0, 4); + err = hipFree(dumb_ptr); + + if (err != hipSuccess) + { + fprintf(stderr, "Enable to create the dumb obj (error code %s)!\n", hipGetErrorString(err)); + return; + } + } #elif OPENMP // OpenMP lib #include @@ -35,7 +49,7 @@ #endif // --- profiling mangement --- -#if defined(CLOCK) || defined(ANDROID) +#if defined(ANDROID) && defined(OPENCL) #define PROFILING_CLOCK #endif @@ -93,7 +107,10 @@ struct GraficCommon{ #else // --- CPU variable --- #endif + float h2d_elapsed_time; float elapsed_time; + float d2h_elapsed_time; + bool profiling_clock = false; }; diff --git a/gpu4s_benchmark/common/cmake/setup.cmake b/gpu4s_benchmark/common/cmake/setup.cmake index 7ba4ed48..7dd5d69e 100644 --- a/gpu4s_benchmark/common/cmake/setup.cmake +++ b/gpu4s_benchmark/common/cmake/setup.cmake @@ -16,7 +16,6 @@ set(ENDIANFLAGS "little" CACHE STRING "ENDIANFLAGS : LITTLENDIAN, BIG set(OPT_FLAG "-O3" CACHE STRING "Compiler optimization level : -O2, -O3, -Ofast") set(CUDA_ARCH "native" CACHE STRING "API CUDA version: native, sm_72-86") set(BLA_VENDOR "OpenBLAS" CACHE STRING "BLAS lib : ATLAS, OpenBLAS") -set(PROFILING "GPU" CACHE STRING "PROFILING mode : GPU, CLOCK") if(NOT BLOCKSIZE) set(BLOCKSIZE 16 CACHE STRING "Block size for tiled kernels") diff --git a/gpu4s_benchmark/common/cmake/showConfig.cmake b/gpu4s_benchmark/common/cmake/showConfig.cmake index 201c7e0f..6cbb4e60 100644 --- a/gpu4s_benchmark/common/cmake/showConfig.cmake +++ b/gpu4s_benchmark/common/cmake/showConfig.cmake @@ -59,7 +59,6 @@ function(showConfig) if(NSTREAMS) message(STATUS " Number of Sreams : ${ColorBold}${NSTREAMS}${ColorReset}") endif() - message(STATUS " Profiling Mode : ${ColorBold}${PROFILING}${ColorReset}") message(STATUS " Optimization Level : ${ColorRed}${OPT_FLAG}${ColorReset}") message(STATUS " CUDA Architecture : ${ColorRed}${CUDA_ARCH}${ColorReset}") message(STATUS " OpenCL Version : ${ColorRed}${OPENCL_VERSION}${ColorReset}") diff --git a/gpu4s_benchmark/convolution_2D_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/convolution_2D_bench/cpu_functions/cpu_functions.h index 0c4bf733..e6e13240 100644 --- a/gpu4s_benchmark/convolution_2D_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/convolution_2D_bench/cpu_functions/cpu_functions.h @@ -54,6 +54,7 @@ struct BenchmarkParameters{ bool mute_messages = false; bool csv_format_timestamp = false; int kernel_size = -1; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; diff --git a/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.cu b/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.cu index e1458e05..c537edf0 100644 --- a/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.cu @@ -6,8 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "cuda_common.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -41,40 +39,29 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, GraficObject* deviceObj = static_cast(device_object); // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; // Allocate the device output vector C err = cudaMalloc((void **)&deviceObj->kernel, size_c_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -87,11 +74,13 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k fprintf(stderr, "Failed to copy vector kernel from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } @@ -99,40 +88,47 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ @@ -141,7 +137,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -151,9 +147,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -161,14 +156,13 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->kernel); + err = cudaFree(deviceObj->kernel); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.h b/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.h deleted file mode 100644 index 4539fbb2..00000000 --- a/gpu4s_benchmark/convolution_2D_bench/cuda/cuda_common.h +++ /dev/null @@ -1,11 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./convolution_2D_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - - diff --git a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda.cu index 84b90158..aa08262a 100644 --- a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda.cu @@ -7,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size) { int size = n; @@ -55,16 +55,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + covolution_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_lib.cu index 7b0b6540..3bd148db 100644 --- a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_lib.cu @@ -1,8 +1,6 @@ #include #include "../benchmark_library.h" - - #define checkCUDNN(expression) \ { \ cudnnStatus_t status = (expression); \ @@ -32,13 +30,15 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const bench_t alf = 1; const bench_t bet = 0; cudnnHandle_t cudnn; + checkCUDNN(cudnnCreate(&cudnn)); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); - checkCUDNN(cudnnCreate(&cudnn)); + // create input tensor cudnnTensorDescriptor_t input_descriptor; checkCUDNN(cudnnCreateTensorDescriptor(&input_descriptor)); @@ -125,13 +125,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, deviceObj->d_B)); - + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cuDNN cudaFree(d_workspace); diff --git a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_opt.cu index 8b26e650..2839dec7 100644 --- a/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/convolution_2D_bench/cuda/lib_cuda_opt.cu @@ -1,21 +1,13 @@ #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size, const int shared_size, const int kernel_rad) { unsigned int size = n; @@ -106,16 +98,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + covolution_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size, size_shared_position, kernel_rad); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.cpp b/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.cpp index 12a2e50d..adcabcc7 100644 --- a/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -36,44 +35,38 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; // Allocate the device output vector C err = hipMalloc((void **)&deviceObj->kernel, size_c_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; + return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -86,50 +79,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k fprintf(stderr, "Failed to copy vector kernel from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -137,7 +139,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -147,9 +149,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); - + + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -157,17 +158,16 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->kernel); + err = hipFree(deviceObj->kernel); if (err != hipSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); + fprintf(stderr, "Failed to free device vector kenerk (error code %s)!\n", hipGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.h b/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.h deleted file mode 100644 index 09f238e1..00000000 --- a/gpu4s_benchmark/convolution_2D_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./convolution_2D_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip.cpp b/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip.cpp index c5b5537a..8646e286 100644 --- a/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int n, const int m, const int w, const int kernel_size) { unsigned int size = n; @@ -55,19 +54,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((covolution_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip_opt.cpp index 4a4ac91b..58bbfec5 100644 --- a/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -98,18 +97,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int kernel_rad = kernel_size / 2; unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((covolution_kernel), dim3(dimGrid), dim3(dimBlock), size_shared , 0, deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size, size_shared_position, kernel_rad); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/main.cpp b/gpu4s_benchmark/convolution_2D_bench/main.cpp index ca500b0c..c26d28a2 100644 --- a/gpu4s_benchmark/convolution_2D_bench/main.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/main.cpp @@ -133,6 +133,14 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + conv_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + conv_bench->profiling_clock = true; + #endif + // init memory device_memory_init(conv_bench, arguments_parameters->size * arguments_parameters->size, arguments_parameters->size * arguments_parameters->size, size_k); // copy memory to device @@ -251,6 +259,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -265,6 +274,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -295,6 +305,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; case 'k' : args +=1; arguments_parameters->kernel_size = atoi(argv[args]);break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -314,4 +325,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl.cpp index be91528d..532e8d4a 100644 --- a/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl.cpp @@ -4,7 +4,6 @@ #include #include "GEN_kernel.hcl" - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w, unsigned int kernel_size){ GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local= BLOCK_SIZE; @@ -35,10 +34,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_conv=cl::Kernel(program,"kernel_matrix_convolution"); kernel_conv.setArg(0,*deviceObj->d_A); @@ -50,11 +50,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_conv.setArg(6,kernel_size); deviceObj->queue->enqueueNDRangeKernel(kernel_conv,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl_opt.cpp index abddf9f8..953e40cc 100644 --- a/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/opencl/lib_opencl_opt.cpp @@ -38,10 +38,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int size_shared = (BLOCK_SIZE + kernel_rad *2 ) * sizeof(bench_t) * (BLOCK_SIZE + kernel_rad *2) * sizeof(bench_t); unsigned int size_shared_position = (BLOCK_SIZE + kernel_rad *2); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_conv=cl::Kernel(program,"kernel_matrix_convolution"); kernel_conv.setArg(0,*deviceObj->d_A); @@ -56,10 +57,12 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_conv.setArg(9, kernel_rad); deviceObj->queue->enqueueNDRangeKernel(kernel_conv,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.cpp index 14baf9ce..349b2a13 100644 --- a/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.cpp @@ -41,7 +41,6 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na deviceObj->evt_copyA = new cl::Event; deviceObj->evt_copyB = new cl::Event; deviceObj->evt_copyC = new cl::Event; - } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ @@ -62,11 +61,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -82,51 +81,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + // Clock profilling start + d2hCLK.start(); + + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); - float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - - #ifdef PROFILING_CLOCK + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -135,7 +142,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.h b/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.h deleted file mode 100644 index 4283cfa8..00000000 --- a/gpu4s_benchmark/convolution_2D_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./convolution_2D_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/correlation_2D/cpu_functions/cpu_functions.h b/gpu4s_benchmark/correlation_2D/cpu_functions/cpu_functions.h index a05ac30e..99a7d4a5 100644 --- a/gpu4s_benchmark/correlation_2D/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/correlation_2D/cpu_functions/cpu_functions.h @@ -57,6 +57,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; diff --git a/gpu4s_benchmark/correlation_2D/cuda/cuda_common.cu b/gpu4s_benchmark/correlation_2D/cuda/cuda_common.cu index a1242180..cf8c7934 100644 --- a/gpu4s_benchmark/correlation_2D/cuda/cuda_common.cu +++ b/gpu4s_benchmark/correlation_2D/cuda/cuda_common.cu @@ -37,73 +37,47 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; // Allocate the device output R value err = cudaMalloc((void **)&deviceObj->d_R, sizeof(result_bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; // Allocate the auxiliar values for matrix A and B - err = cudaMalloc((void **)&deviceObj->mean_A, sizeof(result_bench_t)); - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; err = cudaMalloc((void **)&deviceObj->mean_B, sizeof(result_bench_t)); - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; err = cudaMalloc((void **)&deviceObj->acumulate_value_a_b, sizeof(result_bench_t)); - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; err = cudaMalloc((void **)&deviceObj->acumulate_value_a_a, sizeof(result_bench_t)); - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; + err = cudaMalloc((void **)&deviceObj->acumulate_value_b_b, sizeof(result_bench_t)); - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a, bench_t* h_B, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -118,58 +92,76 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, result_bench_t* h_R){ GraficObject* deviceObj = static_cast(device_object); - - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - - cudaEventRecord(*deviceObj->start_memory_copy_host); result_bench_t acumulate_value_a_a; result_bench_t acumulate_value_a_b; result_bench_t acumulate_value_b_b; - cudaMemcpy(&acumulate_value_a_a, deviceObj->acumulate_value_a_a, sizeof(result_bench_t), cudaMemcpyDeviceToHost); - cudaMemcpy(&acumulate_value_a_b, deviceObj->acumulate_value_a_b, sizeof(result_bench_t), cudaMemcpyDeviceToHost); - cudaMemcpy(&acumulate_value_b_b, deviceObj->acumulate_value_b_b, sizeof(result_bench_t), cudaMemcpyDeviceToHost); + // device -> host + Clock d2hCLK; + + // profilling start + d2hCLK.start(); + cudaEventRecord(*deviceObj->start_memory_copy_host); + + cudaError_t err = cudaMemcpy(&acumulate_value_a_a, deviceObj->acumulate_value_a_a, sizeof(result_bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_a from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + err = cudaMemcpy(&acumulate_value_a_b, deviceObj->acumulate_value_a_b, sizeof(result_bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_b from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + err = cudaMemcpy(&acumulate_value_b_b, deviceObj->acumulate_value_b_b, sizeof(result_bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_b_b from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } *h_R = (result_bench_t)(acumulate_value_a_b / (result_bench_t)(sqrt(acumulate_value_a_a * acumulate_value_b_b))); //cudaMemcpy(h_R, deviceObj->d_R, sizeof(result_bench_t), cudaMemcpyDeviceToHost); + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ @@ -178,7 +170,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -188,9 +180,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -198,7 +189,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); @@ -206,7 +196,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_R); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device R (error code %s)!\n", cudaGetErrorString(err)); @@ -220,6 +209,7 @@ void clean(GraficCommon* device_object){ fprintf(stderr, "Failed to free device mean_A (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree( deviceObj->mean_B); if (err != cudaSuccess) { @@ -233,12 +223,14 @@ void clean(GraficCommon* device_object){ fprintf(stderr, "Failed to free device acumulate_value_a_b (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->acumulate_value_a_a); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device acumulate_value_a_a (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->acumulate_value_b_b); if (err != cudaSuccess) { diff --git a/gpu4s_benchmark/correlation_2D/cuda/cuda_common.h b/gpu4s_benchmark/correlation_2D/cuda/cuda_common.h deleted file mode 100644 index 0fa1048f..00000000 --- a/gpu4s_benchmark/correlation_2D/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./correlation_2D) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/correlation_2D/cuda/lib_cuda.cu b/gpu4s_benchmark/correlation_2D/cuda/lib_cuda.cu index 932b6474..e4e36d55 100644 --- a/gpu4s_benchmark/correlation_2D/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/correlation_2D/cuda/lib_cuda.cu @@ -6,8 +6,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void mean_matrices (const bench_t *A,const bench_t *B,result_bench_t *mean_A ,result_bench_t *mean_B ,const int n){ unsigned int size = n; unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -49,20 +49,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x),ceil(float(n)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + mean_matrices<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->mean_A, deviceObj->mean_B , n); correlation_2D<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_R, deviceObj->mean_A, deviceObj->mean_B,deviceObj->acumulate_value_a_b, deviceObj->acumulate_value_a_a, deviceObj->acumulate_value_b_b, n); + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/correlation_2D/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/correlation_2D/cuda/lib_cuda_opt.cu index dcb7b6b0..81522de0 100644 --- a/gpu4s_benchmark/correlation_2D/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/correlation_2D/cuda/lib_cuda_opt.cu @@ -6,7 +6,6 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 __global__ void mean_matrices(const bench_t *A,const bench_t *B,result_bench_t *mean_A ,result_bench_t *mean_B ,const int n) @@ -126,20 +125,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x),ceil(float(n)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + mean_matrices<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->mean_A, deviceObj->mean_B , n); correlation_2D<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_R, deviceObj->mean_A, deviceObj->mean_B,deviceObj->acumulate_value_a_b, deviceObj->acumulate_value_a_a, deviceObj->acumulate_value_b_b, n); + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/correlation_2D/hip/hip_common.cpp b/gpu4s_benchmark/correlation_2D/hip/hip_common.cpp index 476d6f7c..293db5c9 100644 --- a/gpu4s_benchmark/correlation_2D/hip/hip_common.cpp +++ b/gpu4s_benchmark/correlation_2D/hip/hip_common.cpp @@ -36,74 +36,55 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ - GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + GraficObject* deviceObj = static_cast(device_object); - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; // Allocate the device output R value err = hipMalloc((void **)&deviceObj->d_R, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; // Allocate the auxiliar values for matrix A and B - err = hipMalloc((void **)&deviceObj->mean_A, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; err = hipMalloc((void **)&deviceObj->mean_B, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; err = hipMalloc((void **)&deviceObj->acumulate_value_a_b, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; err = hipMalloc((void **)&deviceObj->acumulate_value_a_a, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; + err = hipMalloc((void **)&deviceObj->acumulate_value_b_b, sizeof(result_bench_t)); - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a, bench_t* h_B, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -118,57 +99,75 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, result_bench_t* h_R){ GraficObject* deviceObj = static_cast(device_object); - - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - - (void)hipEventRecord(*deviceObj->start_memory_copy_host); result_bench_t acumulate_value_a_a; result_bench_t acumulate_value_a_b; result_bench_t acumulate_value_b_b; - hipMemcpy(&acumulate_value_a_a, deviceObj->acumulate_value_a_a, sizeof(result_bench_t), hipMemcpyDeviceToHost); - hipMemcpy(&acumulate_value_a_b, deviceObj->acumulate_value_a_b, sizeof(result_bench_t), hipMemcpyDeviceToHost); - hipMemcpy(&acumulate_value_b_b, deviceObj->acumulate_value_b_b, sizeof(result_bench_t), hipMemcpyDeviceToHost); + // device -> host + Clock d2hCLK; + + // profilling start + d2hCLK.start(); + (void)hipEventRecord(*deviceObj->start_memory_copy_host); + + hipError_t err = hipMemcpy(&acumulate_value_a_a, deviceObj->acumulate_value_a_a, sizeof(result_bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_a from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + err = hipMemcpy(&acumulate_value_a_b, deviceObj->acumulate_value_a_b, sizeof(result_bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_b from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + err = hipMemcpy(&acumulate_value_b_b, deviceObj->acumulate_value_b_b, sizeof(result_bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector acumulate_value_b_b from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } *h_R = (result_bench_t)(acumulate_value_a_b / (result_bench_t)(sqrt(acumulate_value_a_a * acumulate_value_b_b))); //hipMemcpy(h_R, deviceObj->d_R, sizeof(result_bench_t), hipMemcpyDeviceToHost); + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ @@ -177,7 +176,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -187,9 +186,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -197,7 +195,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); @@ -205,7 +202,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_R); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device R (error code %s)!\n", hipGetErrorString(err)); @@ -232,12 +228,14 @@ void clean(GraficCommon* device_object){ fprintf(stderr, "Failed to free device acumulate_value_a_b (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->acumulate_value_a_a); if (err != hipSuccess) { fprintf(stderr, "Failed to free device acumulate_value_a_a (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->acumulate_value_b_b); if (err != hipSuccess) { diff --git a/gpu4s_benchmark/correlation_2D/hip/hip_common.h b/gpu4s_benchmark/correlation_2D/hip/hip_common.h deleted file mode 100644 index 3b6edcc4..00000000 --- a/gpu4s_benchmark/correlation_2D/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./correlation_2D) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/correlation_2D/hip/lib_hip.cpp b/gpu4s_benchmark/correlation_2D/hip/lib_hip.cpp index e762bd46..949c56e2 100644 --- a/gpu4s_benchmark/correlation_2D/hip/lib_hip.cpp +++ b/gpu4s_benchmark/correlation_2D/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void mean_matrices (const bench_t *A,const bench_t *B,result_bench_t *mean_A ,result_bench_t *mean_B ,const int n){ unsigned int size = n; unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -51,20 +50,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x),ceil(float(n)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL(mean_matrices, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->mean_A, deviceObj->mean_B , n); hipLaunchKernelGGL(correlation_2D, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->d_R, deviceObj->mean_A, deviceObj->mean_B,deviceObj->acumulate_value_a_b, deviceObj->acumulate_value_a_a, deviceObj->acumulate_value_b_b, n); + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/correlation_2D/hip/lib_hip_opt.cpp b/gpu4s_benchmark/correlation_2D/hip/lib_hip_opt.cpp index df75d3e8..4cbf7a49 100644 --- a/gpu4s_benchmark/correlation_2D/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/correlation_2D/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -8,7 +7,6 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 __global__ void mean_matrices(const bench_t *A,const bench_t *B,result_bench_t *mean_A ,result_bench_t *mean_B ,const int n) @@ -127,20 +125,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x),ceil(float(n)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL(mean_matrices, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->mean_A, deviceObj->mean_B , n); hipLaunchKernelGGL(correlation_2D, dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->d_R, deviceObj->mean_A, deviceObj->mean_B,deviceObj->acumulate_value_a_b, deviceObj->acumulate_value_a_a, deviceObj->acumulate_value_b_b, n); + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/correlation_2D/main.cpp b/gpu4s_benchmark/correlation_2D/main.cpp index d15a9b21..e5c38626 100644 --- a/gpu4s_benchmark/correlation_2D/main.cpp +++ b/gpu4s_benchmark/correlation_2D/main.cpp @@ -134,6 +134,15 @@ int main(int argc, char *argv[]){ if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + correlation_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + correlation_bench->profiling_clock = true; + #endif + // init memory device_memory_init(correlation_bench, size_A , size_B ); // copy memory to device @@ -217,6 +226,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -231,6 +241,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -261,6 +272,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -274,4 +286,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/correlation_2D/opencl/lib_opencl.cpp b/gpu4s_benchmark/correlation_2D/opencl/lib_opencl.cpp index 4822f135..bd1cc93e 100644 --- a/gpu4s_benchmark/correlation_2D/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/correlation_2D/opencl/lib_opencl.cpp @@ -34,10 +34,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_mean=cl::Kernel(program,"mean_matrices"); kernel_mean.setArg(0,*deviceObj->d_A); @@ -59,10 +60,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ kernel.setArg(8,n); deviceObj->queue->enqueueNDRangeKernel(kernel,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/correlation_2D/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/correlation_2D/opencl/lib_opencl_opt.cpp index 82093c1a..77d27a24 100644 --- a/gpu4s_benchmark/correlation_2D/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/correlation_2D/opencl/lib_opencl_opt.cpp @@ -36,10 +36,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_mean=cl::Kernel(program,"mean_matrices"); kernel_mean.setArg(0,*deviceObj->d_A); @@ -61,10 +62,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ kernel.setArg(8,n); deviceObj->queue->enqueueNDRangeKernel(kernel,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/correlation_2D/opencl/opencl_common.cpp b/gpu4s_benchmark/correlation_2D/opencl/opencl_common.cpp index 60b7eab7..0f27294c 100644 --- a/gpu4s_benchmark/correlation_2D/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/correlation_2D/opencl/opencl_common.cpp @@ -8,7 +8,6 @@ #include "../benchmark_library.h" #include - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -83,11 +82,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a, bench_t* h_B, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -103,57 +102,75 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, result_bench_t* h_R){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); result_bench_t acumulate_value_a_a; result_bench_t acumulate_value_a_b; result_bench_t acumulate_value_b_b; - deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_a_a,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_a_a, NULL, deviceObj->evt_copyAA); - deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_a_b,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_a_b, NULL, deviceObj->evt_copyAB); - deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_b_b,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_b_b, NULL, deviceObj->evt_copyBB); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_a_a,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_a_a, NULL, deviceObj->evt_copyAA); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_a from device to host (OpenCL error code %d)!\n", err); + return; + } + err = deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_a_b,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_a_b, NULL, deviceObj->evt_copyAB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector acumulate_value_a_b from device to host (OpenCL error code %d)!\n", err); + return; + } + err = deviceObj->queue->enqueueReadBuffer(*deviceObj->acumulate_value_b_b,CL_TRUE,0,sizeof(result_bench_t),&acumulate_value_b_b, NULL, deviceObj->evt_copyBB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector acumulate_value_b_b from device to host (OpenCL error code %d)!\n", err); + return; + } deviceObj->evt_copyBB->wait(); *h_R = (result_bench_t)(acumulate_value_a_b / (result_bench_t)(sqrt(acumulate_value_a_a * acumulate_value_b_b))); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyBB->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - elapsed += deviceObj->evt_mean->getProfilingInfo() - deviceObj->evt_mean->getProfilingInfo(); - elapsed_d_h = deviceObj->evt_copyAA->getProfilingInfo() - deviceObj->evt_copyAA->getProfilingInfo(); - elapsed_d_h += deviceObj->evt_copyAB->getProfilingInfo() - deviceObj->evt_copyAB->getProfilingInfo(); - elapsed_d_h += deviceObj->evt_copyBB->getProfilingInfo() - deviceObj->evt_copyBB->getProfilingInfo(); - - - #ifdef PROFILING_CLOCK + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + elapsed += deviceObj->evt_mean->getProfilingInfo() - deviceObj->evt_mean->getProfilingInfo(); + + elapsed_d_h = deviceObj->evt_copyAA->getProfilingInfo() - deviceObj->evt_copyAA->getProfilingInfo(); + elapsed_d_h += deviceObj->evt_copyAB->getProfilingInfo() - deviceObj->evt_copyAB->getProfilingInfo(); + elapsed_d_h += deviceObj->evt_copyBB->getProfilingInfo() - deviceObj->evt_copyBB->getProfilingInfo(); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0, current_time); @@ -161,7 +178,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/correlation_2D/opencl/opencl_common.h b/gpu4s_benchmark/correlation_2D/opencl/opencl_common.h deleted file mode 100644 index 6695dd49..00000000 --- a/gpu4s_benchmark/correlation_2D/opencl/opencl_common.h +++ /dev/null @@ -1,18 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./correlation_2D) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cpu_functions/cpu_functions.h index b2734cd7..211a3ca1 100644 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cpu_functions/cpu_functions.h @@ -44,7 +44,8 @@ struct BenchmarkParameters{ bool csv_format_timestamp = false; char input_file[100] = ""; char output_file[100] = ""; -}; + + bool profiling_clock = false;}; bool FFT2D(COMPLEX **c,int n,int dir, COMPLEX **exit); bool compare_vectors(COMPLEX **host, COMPLEX **device, int64_t size); diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.cpp b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.cpp deleted file mode 100644 index 568fc297..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.cpp (./fast_fourier_transform_2D_bench) - * @brief Common CUDA platform initialization, device setup, - * profiling timer evaluation, and generic cleanup routines. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#include "../benchmark_library.h" diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.h b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.h deleted file mode 100644 index aa44d2cf..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/cuda_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./fast_fourier_transform_2D_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/lib_cuda_lib.cu index 24f7c888..51acc043 100644 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/fast_fourier_transform_2D_bench/cuda/lib_cuda_lib.cu @@ -1,13 +1,5 @@ #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * @@ -44,26 +36,21 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, int64_t size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; + // Allocate the device input vector A - err = cudaMalloc((void **)&deviceObj->d_A, (size_b_matrix * size_b_matrix) * sizeof(bench_cuda_complex)); + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, (size_b_matrix * size_b_matrix) * sizeof(bench_cuda_complex)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, (size_b_matrix * size_b_matrix) * sizeof(bench_cuda_complex)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, COMPLEX **h_B,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; + // flat the complex buffer bench_cuda_complex *h_signal = (bench_cuda_complex *)malloc(sizeof(bench_cuda_complex) * (size * size)); for (unsigned int i = 0; i < (size); ++i){ for (unsigned int j = 0; j < (size); ++j){ @@ -72,34 +59,37 @@ void copy_memory_to_device(GraficCommon* device_object, COMPLEX **h_B,int64_t si } } - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); - err = cudaMemcpy(deviceObj->d_A, h_signal, sizeof(bench_cuda_complex) * (size * size), cudaMemcpyHostToDevice); + + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_signal, sizeof(bench_cuda_complex) * (size * size), cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } -void execute_kernel(GraficCommon* device_object, int64_t size){ - -GraficObject* deviceObj = static_cast(device_object); +void execute_kernel(GraficCommon* device_object, int64_t size){ + GraficObject* deviceObj = static_cast(device_object); cufftHandle plan; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); #ifdef FLOAT @@ -110,32 +100,40 @@ GraficObject* deviceObj = static_cast(device_object); cufftExecZ2Z(plan, (cufftDoubleComplex *)deviceObj->d_A, (cufftDoubleComplex *)deviceObj->d_B, CUFFT_FORWARD); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); cufftDestroy(plan); - } void copy_memory_to_host(GraficCommon* device_object, COMPLEX **h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); bench_cuda_complex *h_signal = (bench_cuda_complex *)malloc(sizeof(bench_cuda_complex) * (size*size)); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_signal, deviceObj->d_B, (size*size) * sizeof(bench_cuda_complex), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_signal, deviceObj->d_B, (size*size) * sizeof(bench_cuda_complex), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); for (unsigned int i = 0; i < (size); ++i){ for (unsigned int j = 0; j < (size); ++j){ @@ -147,24 +145,24 @@ void copy_memory_to_host(GraficCommon* device_object, COMPLEX **h_B, int64_t siz float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -172,7 +170,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -182,10 +180,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); @@ -193,7 +189,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/main.cpp b/gpu4s_benchmark/fast_fourier_transform_2D_bench/main.cpp index 91a1f1c0..6e99e3c6 100644 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/main.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_2D_bench/main.cpp @@ -91,6 +91,15 @@ int main(int argc, char *argv[]){ if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + fft_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + fft_bench->profiling_clock = true; + #endif + // init memory device_memory_init(fft_bench, size_A); // copy memory to device @@ -187,6 +196,7 @@ void print_usage(const char * appName) printf(" -q: prints input\n"); printf(" -d: selects GPU\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -201,6 +211,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } @@ -229,6 +240,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atol(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -240,4 +252,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par } // specific return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/fast_fourier_transform_2D_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/fast_fourier_transform_2D_bench/opencl/lib_opencl_lib.cpp index 94602f69..5ee2738b 100644 --- a/gpu4s_benchmark/fast_fourier_transform_2D_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_2D_bench/opencl/lib_opencl_lib.cpp @@ -3,17 +3,6 @@ #include "../benchmark_library.h" #include "vkFFT.h" - -// kernel time execution -Clock kernelCLK; -#ifdef PROFILING_CLOCK - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - -//#define BLOCK_SIZE 32 void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -47,8 +36,6 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na deviceObj->evt = new cl::Event; deviceObj->evt_copyB = new cl::Event; deviceObj->evt_copyBr = new cl::Event; - - } bool device_memory_init(GraficCommon* device_object, int64_t size){ @@ -78,17 +65,20 @@ void copy_memory_to_device(GraficCommon *device_object, COMPLEX **h_B,int64_t si } } - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // Clock profilling start + h2dCLK.start(); // copy memory host -> device deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size*size*2, h_signal, NULL, deviceObj->evt_copyB); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); free(h_signal); } @@ -116,6 +106,8 @@ void execute_kernel(GraficCommon* device_object, int64_t size) { config.doublePrecision = 1; #endif + // kernel time execution + Clock kernelCLK; // --- init --- kernelCLK.start(); // Start clock @@ -140,16 +132,24 @@ void copy_memory_to_host(GraficCommon* device_object, COMPLEX **h_B, int64_t siz GraficObject* deviceObj = static_cast(device_object); bench_t *h_signal = (bench_t *)malloc(sizeof(bench_t) * size * size * 2); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size*size * 2,h_signal, NULL, deviceObj->evt_copyBr); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size*size * 2, h_signal, NULL, deviceObj->evt_copyBr); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); for (int i=0; i(device_object); deviceObj->evt_copyBr->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - - #ifdef PROFILING_CLOCK - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + + if (deviceObj->profiling_clock) + { + // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0,elapsed_d_h / 1000000.0, current_time); @@ -187,7 +187,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/fast_fourier_transform_bench/cpu_functions/cpu_functions.h index 8d9cbe8e..5db5159d 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/fast_fourier_transform_bench/cpu_functions/cpu_functions.h @@ -57,7 +57,8 @@ struct BenchmarkParameters{ bool csv_format_timestamp = false; char input_file[100] = ""; char output_file[100] = ""; -}; + + bool profiling_clock = false;}; void fft_function(bench_t* data,int64_t nn); bool compare_vectors(const bench_t* host,const bench_t* device, const int64_t size); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.cu b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.cu index a4a4e2d8..daee47f7 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.cu @@ -36,84 +36,87 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, int64_t size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; + // Allocate the device input vector B - err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + cudaError_t err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } // Allocate the device reverse vector Br err = cudaMalloc((void **)&deviceObj->d_Br, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_B,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); - err = cudaMemcpy(deviceObj->d_B, h_B, sizeof(bench_t) * size, cudaMemcpyHostToDevice); + + cudaError_t err = cudaMemcpy(deviceObj->d_B, h_B, sizeof(bench_t) * size, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_B, deviceObj->d_Br, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_B, deviceObj->d_Br, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); //Wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -121,7 +124,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -131,17 +134,16 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_B); + cudaError_t err = cudaFree(deviceObj->d_B); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->d_Br); + err = cudaFree(deviceObj->d_Br); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector Br (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.h b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.h deleted file mode 100644 index f3ab28eb..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./fast_fourier_transform_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda.cu index f0fe6765..c3f00482 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda.cu @@ -51,11 +51,13 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ bench_t wtemp, wpr, wpi, theta, wr, wi; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + // reorder kernel binary_reverse_kernel<<>>(deviceObj->d_B, deviceObj->d_Br, size, (int64_t)log2(size)); // Synchronize @@ -100,11 +102,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ } + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_lib.cu index 171db755..ceab0941 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_lib.cu @@ -8,15 +8,13 @@ */ void execute_kernel(GraficCommon* device_object, int64_t size){ - -GraficObject* deviceObj = static_cast(device_object); - + GraficObject* deviceObj = static_cast(device_object); cufftHandle plan; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); #ifdef FLOAT @@ -27,12 +25,13 @@ GraficObject* deviceObj = static_cast(device_object); cufftExecZ2Z(plan, (cufftDoubleComplex *)deviceObj->d_B, (cufftDoubleComplex *)deviceObj->d_Br, CUFFT_FORWARD); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); cufftDestroy(plan); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_opt.cu index b6e339d5..c170e044 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/fast_fourier_transform_bench/cuda/lib_cuda_opt.cu @@ -68,11 +68,13 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ bench_t wtemp, wpr, wpi, theta; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + // reorder kernel binary_reverse_kernel<<>>(deviceObj->d_B, deviceObj->d_Br, size, (int64_t)log2(size)); // Synchronize @@ -108,11 +110,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ } + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.cpp index 4db0dd15..e4692c89 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ @@ -38,84 +37,93 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, int64_t size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - // Allocate the device input vector B - err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector B + hipError_t err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; + // Allocate the device reverse vector Br err = hipMalloc((void **)&deviceObj->d_Br, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_B,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); - err = hipMemcpy(deviceObj->d_B, h_B, sizeof(bench_t) * size, hipMemcpyHostToDevice); + + hipError_t err = hipMemcpy(deviceObj->d_B, h_B, sizeof(bench_t) * size, hipMemcpyHostToDevice); if (err != hipSuccess) { fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_B, deviceObj->d_Br, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_B, deviceObj->d_Br, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector Br from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -123,7 +131,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -133,17 +141,15 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - err = hipFree(deviceObj->d_B); + hipError_t err = hipFree(deviceObj->d_B); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->d_Br); + err = hipFree(deviceObj->d_Br); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector Br (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.h b/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.h deleted file mode 100644 index 0a3f7ca0..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_bench/hip/hip_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./fast_fourier_transform_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip.cpp index e7d9c032..aff65f57 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -50,14 +49,14 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ dim3 dimGrid_reverse(ceil(float(size)/dimBlock_reverse.x)); dim3 dimBlock(0); dim3 dimGrid(0); - bench_t wtemp, wpr, wpi, theta, wr, wi; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + // reorder kernel hipLaunchKernelGGL((binary_reverse_kernel), dim3(dimGrid_reverse), dim3(dimBlock_reverse), 0, 0, deviceObj->d_B, deviceObj->d_Br, size, (int64_t)log2(size)); // Synchronize @@ -102,11 +101,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ } + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip_opt.cpp index 1f24a6eb..37205399 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" /** @@ -67,14 +66,14 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ dim3 dimGrid_reverse(ceil(float(size)/dimBlock_reverse.x)); dim3 dimBlock(0); dim3 dimGrid(0); - bench_t wtemp, wpr, wpi, theta; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + // reorder kernel hipLaunchKernelGGL((binary_reverse_kernel), dim3(dimGrid_reverse), dim3(dimBlock_reverse), 0, 0, deviceObj->d_B, deviceObj->d_Br, size, (int64_t)log2(size)); // Synchronize @@ -110,11 +109,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ } + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/main.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/main.cpp index 5c0184b6..238124bb 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/main.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/main.cpp @@ -92,6 +92,15 @@ int main(int argc, char *argv[]){ if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + fft_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + fft_bench->profiling_clock = true; + #endif + // init memory device_memory_init(fft_bench, size_B); // copy memory to device @@ -190,6 +199,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -204,6 +214,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -231,6 +242,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -244,4 +256,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl.cpp index 8235d074..28b41791 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl.cpp @@ -3,8 +3,6 @@ #include "../benchmark_library.h" #include "GEN_kernel.hcl" - - void execute_kernel(GraficCommon* device_object, int64_t size){ GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local= BLOCK_SIZE; @@ -37,11 +35,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ exit(1); } + // kernel time execution + Clock kernelCLK; + // Clock profilling start deviceObj->queue->finish(); // Clear queue to ensure accurate start - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + kernelCLK.start(); //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt); @@ -106,9 +105,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_end); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.end(); - #endif + // Wait for completion before stopping the clock + deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_lib.cpp index ae14e4df..67fb35fd 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_lib.cpp @@ -4,9 +4,6 @@ #include "Clock.h" #include "vkFFT.h" -// kernel time execution -Clock kernelCLK; - void execute_kernel(GraficCommon* device_object, int64_t size){ GraficObject* deviceObj = static_cast(device_object); @@ -30,6 +27,8 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ config.doublePrecision = 1; #endif + // kernel time execution + Clock kernelCLK; // --- init --- kernelCLK.start(); // Start clock @@ -45,6 +44,8 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ deviceObj->queue->finish(); kernelCLK.end(); // End clock + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); // --- cleanup --- deleteVkFFT(&app); @@ -53,24 +54,24 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyBr->wait(); - float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - - elapsed = kernelCLK.getElapsedNS(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - #ifdef PROFILING_CLOCK + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n",elapsed_h_d / 1000000.0, elapsed / 1000000.0, elapsed_d_h / 1000000.0, current_time); @@ -78,7 +79,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0 ); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_opt.cpp index ec478656..79d4eccd 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/lib_opencl_opt.cpp @@ -36,10 +36,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ exit(1); } + // kernel time execution + Clock kernelCLK; + + // Clock profilling start deviceObj->queue->finish(); // Clear queue to ensure accurate start - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + kernelCLK.start(); //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt); @@ -94,9 +96,12 @@ void execute_kernel(GraficCommon* device_object, int64_t size){ //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_end); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.end(); - #endif + // Wait for completion before stopping the clock + deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.cpp index e4036cbf..fe3a6383 100644 --- a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.cpp @@ -49,22 +49,22 @@ bool device_memory_init(GraficCommon* device_object, int64_t size){ GraficObject* deviceObj = static_cast(device_object); cl_int err; - deviceObj->d_B = new cl::Buffer(*deviceObj->context,CL_MEM_READ_ONLY ,sizeof(bench_t)*size, nullptr, &err); - if (err != CL_SUCCESS) return false; + deviceObj->d_B = new cl::Buffer(*deviceObj->context,CL_MEM_READ_ONLY ,sizeof(bench_t)*size, nullptr, &err); + if (err != CL_SUCCESS) return false; - deviceObj->d_Br = new cl::Buffer(*deviceObj->context,CL_MEM_READ_WRITE ,sizeof(bench_t)*size, nullptr, &err); - if (err != CL_SUCCESS) return false; - // inicialice Arrays - return true; + deviceObj->d_Br = new cl::Buffer(*deviceObj->context,CL_MEM_READ_WRITE ,sizeof(bench_t)*size, nullptr, &err); + if (err != CL_SUCCESS) return false; + // inicialice Arrays + return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_B,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size, h_B, NULL, deviceObj->evt_copyB); if (err != CL_SUCCESS) @@ -73,49 +73,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_B,int64_t siz return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_Br,CL_TRUE,0,sizeof(bench_t)*size,h_B, NULL, deviceObj->evt_copyBr); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_Br, CL_TRUE, 0, sizeof(bench_t)*size, h_B, NULL, deviceObj->evt_copyBr); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } __attribute__((weak)) // lib_opencl_lib need is specefic version to force measure time with clock float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyBr->wait(); - float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt_end->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt_end->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -124,7 +133,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0, elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.h b/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.h deleted file mode 100644 index 52be2ae5..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./fast_fourier_transform_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/fast_fourier_transform_window_bench/cpu_functions/cpu_functions.h index d0d8c901..705c0885 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/cpu_functions/cpu_functions.h @@ -56,6 +56,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.cu b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.cu index 090d57fa..4321bc95 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.cu @@ -36,83 +36,86 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, int64_t size_a_array, int64_t size_b_array){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; + // Allocate the device input vector A - err = cudaMalloc((void **)&deviceObj->d_A, size_a_array * sizeof(bench_t)); + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_array * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } // Allocate the device reverse vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_array * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); - err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size, cudaMemcpyHostToDevice); + + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -120,7 +123,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -130,20 +133,18 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->d_B); + err = cudaFree(deviceObj->d_B); if (err != cudaSuccess) { - fprintf(stderr, "Failed to free device vector Br (error code %s)!\n", cudaGetErrorString(err)); + fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.h b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.h deleted file mode 100644 index 2845b74a..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/cuda_common.h +++ /dev/null @@ -1,18 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./fast_fourier_transform_window_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda.cu index 5e122df9..68558dd5 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda.cu @@ -97,24 +97,24 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit } void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ - -GraficObject* deviceObj = static_cast(device_object); - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + for (unsigned int i = 0; i < (size * 2 - window + 1); i+=2){ aux_execute_kernel(device_object, window, i); } + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_lib.cu index bfb1c36b..91a83da9 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_lib.cu @@ -59,25 +59,29 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ GraficObject* deviceObj = static_cast(device_object); bench_cuda_complex* d_A = (bench_cuda_complex*)deviceObj->d_A; bench_cuda_complex* d_B = (bench_cuda_complex*)deviceObj->d_B; + cufftHandle plan; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); - cufftHandle plan; + for (unsigned int i = 0; i < (size * 2 - window + 1); i+=1){ aux_execute_kernel(device_object, window, d_A, d_B, &plan); d_B += window; ++d_A; } - cufftDestroy(plan); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); + + cufftDestroy(plan); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_opt.cu index 76cfdb4a..0a3c1bbb 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/cuda/lib_cuda_opt.cu @@ -109,20 +109,23 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + for (unsigned int i = 0; i < (size * 2 - window + 1); i+=2){ aux_execute_kernel(device_object, window, i); } + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.cpp index 305f4212..6d2055ed 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -37,83 +36,92 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, int64_t size_a_array, int64_t size_b_array){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - // Allocate the device input vector A - err = hipMalloc((void **)&deviceObj->d_A, size_a_array * sizeof(bench_t)); - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_array * sizeof(bench_t)); + if (err != hipSuccess) return false; + // Allocate the device reverse vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_array * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); - err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size, hipMemcpyHostToDevice); + + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size, hipMemcpyHostToDevice); if (err != hipSuccess) { fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ - GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); // wait (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -121,7 +129,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -131,17 +139,15 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->d_B); + err = hipFree(deviceObj->d_B); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector Br (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.h b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.h deleted file mode 100644 index 5ebd0870..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./fast_fourier_transform_window_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip.cpp index 6e967904..023d9e2e 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -98,24 +97,24 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit } void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ - -GraficObject* deviceObj = static_cast(device_object); - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + for (unsigned int i = 0; i < (size * 2 - window + 1); i+=2){ aux_execute_kernel(device_object, window, i); } + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip_opt.cpp index 21d160db..63cde6b6 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" /** @@ -67,7 +66,6 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit dim3 dimGrid_reverse(ceil(float(size)/dimBlock_reverse.x)); dim3 dimBlock(0); dim3 dimGrid(0); - bench_t wtemp, wpr, wpi, theta; // reorder kernel @@ -108,21 +106,23 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit } void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + for (unsigned int i = 0; i < (size * 2 - window + 1); i+=2){ aux_execute_kernel(device_object, window, i); } + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/main.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/main.cpp index 724fc6a7..1e0f7756 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/main.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/main.cpp @@ -92,6 +92,15 @@ int main(int argc, char *argv[]){ if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + fft_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + fft_bench->profiling_clock = true; + #endif + // init memory device_memory_init(fft_bench, size_A ,size_B); // copy memory to device @@ -192,6 +201,7 @@ void print_usage(const char * appName) printf(" -q: prints input\n"); printf(" -d: selects GPU\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -207,6 +217,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } @@ -238,6 +249,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); //TODO FIX with final version of input files break; case 's' : args +=1; arguments_parameters->size = atol(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -254,4 +266,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par return ERROR_ARGUMENTS; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl.cpp index f18d519f..e2dbfdcd 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl.cpp @@ -3,7 +3,6 @@ #include "../benchmark_library.h" #include "GEN_kernel.hcl" - void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t position, cl::Program program){ GraficObject* deviceObj = static_cast(device_object); size = size / 2; @@ -106,9 +105,11 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ exit(1); } - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt); @@ -120,8 +121,11 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_end); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.end(); - #endif + // Wait for completion before stopping the clock + deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_lib.cpp index 3ed604a8..221439dc 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_lib.cpp @@ -1,12 +1,8 @@ // OpenCL lib code #include #include "../benchmark_library.h" -#include "Clock.h" #include "vkFFT.h" -// kernel time execution -Clock kernelCLK; - void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ GraficObject* deviceObj = static_cast(device_object); @@ -31,6 +27,8 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ config.doublePrecision = 1; #endif + // kernel time execution + Clock kernelCLK; // --- init --- kernelCLK.start(); // Start clock @@ -58,6 +56,8 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ deviceObj->queue->finish(); kernelCLK.end(); // End clock + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); // --- cleanup --- deleteVkFFT(&app); @@ -68,23 +68,22 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyBr->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = kernelCLK.getElapsedNS(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - // --- select the profiling message --- - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -93,7 +92,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_opt.cpp index 8ac401b4..fc62fec1 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/lib_opencl_opt.cpp @@ -20,7 +20,6 @@ void aux_execute_kernel(GraficCommon* device_object, int64_t size, int64_t posit global_reverse = cl::NDRange(size); } - //cl::NDRange local(x_local, y_local); //cl::NDRange global(n, w); @@ -89,9 +88,11 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt); @@ -103,9 +104,12 @@ void execute_kernel(GraficCommon* device_object, int64_t window, int64_t size){ //FIX : GPU profiling use opencl marker deviceObj->queue->enqueueMarkerWithWaitList(NULL, deviceObj->evt_end); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.end(); - #endif + // Wait for completion before stopping the clock + deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.cpp index 71165424..014a6b38 100644 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.cpp @@ -7,7 +7,6 @@ * ======================================================================= */ #include "../benchmark_library.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -60,10 +59,11 @@ bool device_memory_init(GraficCommon* device_object, int64_t size_a_array, int6 void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A,int64_t size){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size, h_A, NULL, deviceObj->evt_copyB); if (err != CL_SUCCESS) @@ -72,49 +72,57 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A,int64_t siz return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_B, NULL, deviceObj->evt_copyBr); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_B, NULL, deviceObj->evt_copyBr); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } __attribute__((weak)) float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyBr->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt_end->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - // --- select the profiling message --- - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + elapsed = deviceObj->evt_end->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyBr->getProfilingInfo() - deviceObj->evt_copyBr->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0, elapsed_d_h / 1000000.0, current_time); @@ -122,7 +130,7 @@ if (csv_format_timestamp){ else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0, elapsed / 1000000.0, elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.h b/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.h deleted file mode 100644 index de5eeb96..00000000 --- a/gpu4s_benchmark/fast_fourier_transform_window_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./fast_fourier_transform_window_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/finite_impulse_response_filter/cpu_functions/cpu_functions.h b/gpu4s_benchmark/finite_impulse_response_filter/cpu_functions/cpu_functions.h index 9da5bc4d..3b411473 100644 --- a/gpu4s_benchmark/finite_impulse_response_filter/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/finite_impulse_response_filter/cpu_functions/cpu_functions.h @@ -54,6 +54,7 @@ struct BenchmarkParameters{ bool mute_messages = false; bool csv_format_timestamp = false; int kernel_size = 3; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; diff --git a/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.cpp b/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.cpp deleted file mode 100644 index 463548c4..00000000 --- a/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.cpp (./finite_impulse_response_filter) - * @brief Common CUDA platform initialization, device setup, - * profiling timer evaluation, and generic cleanup routines. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#include "../benchmark_library.h" diff --git a/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.h b/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.h deleted file mode 100644 index c0864940..00000000 --- a/gpu4s_benchmark/finite_impulse_response_filter/cuda/cuda_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./finite_impulse_response_filter) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/finite_impulse_response_filter/cuda/lib_cuda.cu b/gpu4s_benchmark/finite_impulse_response_filter/cuda/lib_cuda.cu index 7d8d0192..fd1c6e6e 100644 --- a/gpu4s_benchmark/finite_impulse_response_filter/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/finite_impulse_response_filter/cuda/lib_cuda.cu @@ -1,21 +1,13 @@ #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int output_size, const int size, const int w, const int kernel_size) { int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -69,118 +61,121 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ -GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; // Allocate the device output vector C err = cudaMalloc((void **)&deviceObj->kernel, size_c_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaMemcpy(deviceObj->kernel, kernel, sizeof(bench_t) * size_b, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector kernel from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } + void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w, unsigned int kernel_size){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE); //FIX: Calculate the dimgrid with int to not loose precision dim3 dimGrid((n + dimBlock.x - 1) / dimBlock.x); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + covolution_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -188,7 +183,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -198,9 +193,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -208,17 +202,16 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->kernel); - if (err != cudaSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); + fprintf(stderr, "Failed to free device vector kernel (error code %s)!\n", cudaGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.cpp b/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.cpp deleted file mode 100644 index 826e7616..00000000 --- a/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file hip_common.cpp (./finite_impulse_response_filter) - * @brief Common HIP platform initialization, device setup, - * profiling timer evaluation, and generic cleanup routines. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#include "../benchmark_library.h" diff --git a/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.h b/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.h deleted file mode 100644 index 1b460d0c..00000000 --- a/gpu4s_benchmark/finite_impulse_response_filter/hip/hip_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./finite_impulse_response_filter) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/finite_impulse_response_filter/hip/lib_hip.cpp b/gpu4s_benchmark/finite_impulse_response_filter/hip/lib_hip.cpp index 22fd43d2..d17a91fb 100644 --- a/gpu4s_benchmark/finite_impulse_response_filter/hip/lib_hip.cpp +++ b/gpu4s_benchmark/finite_impulse_response_filter/hip/lib_hip.cpp @@ -1,22 +1,13 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 -__global__ void + + __global__ void covolution_kernel(const bench_t *A, bench_t *B, const bench_t *kernel,const int output_size, const int size, const int w, const int kernel_size) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -69,44 +60,38 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ - -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + GraficObject* deviceObj = static_cast(device_object); - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; // Allocate the device output vector C err = hipMalloc((void **)&deviceObj->kernel, size_c_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -119,11 +104,13 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k fprintf(stderr, "Failed to copy vector kernel from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w, unsigned int kernel_size){ @@ -131,58 +118,68 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dim3 dimBlock(BLOCK_SIZE); //FIX: Calculate the dimgrid with int to not loose precision dim3 dimGrid((n + dimBlock.x - 1) / dimBlock.x); + // kernel time execution + Clock kernelCLK; - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((covolution_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->kernel, n, m, w, kernel_size); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ - GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + GraficObject* deviceObj = static_cast(device_object); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h, current_time); @@ -190,7 +187,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -200,9 +197,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_terr = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -210,17 +206,16 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } - err = hipFree(deviceObj->kernel); + err = hipFree(deviceObj->kernel); if (err != hipSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); + fprintf(stderr, "Failed to free device vector kernel (error code %s)!\n", hipGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/finite_impulse_response_filter/main.cpp b/gpu4s_benchmark/finite_impulse_response_filter/main.cpp index 0403bfa9..be06f772 100644 --- a/gpu4s_benchmark/finite_impulse_response_filter/main.cpp +++ b/gpu4s_benchmark/finite_impulse_response_filter/main.cpp @@ -129,6 +129,14 @@ int main(int argc, char *argv[]) printf("Using device: %s\n", device); } + // Update profiling clock mode + fir_bench->profiling_clock = arguments_parameters->profiling_clock; + + // If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + fir_bench->profiling_clock = true; + #endif + // init memory device_memory_init(fir_bench, arguments_parameters->size , size_B , size_k); // copy memory to device @@ -236,6 +244,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -250,6 +259,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -283,6 +293,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; case 'k' : args +=1; arguments_parameters->kernel_size = atoi(argv[args]);break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } diff --git a/gpu4s_benchmark/finite_impulse_response_filter/opencl/lib_opencl.cpp b/gpu4s_benchmark/finite_impulse_response_filter/opencl/lib_opencl.cpp index 24ba40ff..3de0d9ba 100644 --- a/gpu4s_benchmark/finite_impulse_response_filter/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/finite_impulse_response_filter/opencl/lib_opencl.cpp @@ -4,15 +4,6 @@ #include #include "kernel.cl" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - -//#define BLOCK_SIZE 256 void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -69,11 +60,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* kernel, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device - - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -89,11 +80,11 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* k return; } + // Clock profilling end + h2dCLK.end(); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } @@ -126,10 +117,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_conv=cl::Kernel(program,"kernel_vector_convolution"); kernel_conv.setArg(0,*deviceObj->d_A); @@ -142,49 +134,59 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, deviceObj->queue->enqueueNDRangeKernel(kernel_conv,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -193,7 +195,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format,bool csv_for else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/finite_impulse_response_filter/opencl/opencl_common.h b/gpu4s_benchmark/finite_impulse_response_filter/opencl/opencl_common.h deleted file mode 100644 index 4134a689..00000000 --- a/gpu4s_benchmark/finite_impulse_response_filter/opencl/opencl_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./finite_impulse_response_filter) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/matrix_multiplication_bench/cpu_functions/cpu_functions.h index 8b027c33..9688694d 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/matrix_multiplication_bench/cpu_functions/cpu_functions.h @@ -105,6 +105,7 @@ struct BenchmarkParameters{ char input_file_A[100] = ""; char input_file_B[100] = ""; char output_file[100] = ""; + bool profiling_clock = false; }; void matrix_multiplication(const bench_t* A, const bench_t* B, bench_t* C,const unsigned int n, const unsigned int m, const unsigned int w ); diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.cu b/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.cu index 8705d19d..aa95da06 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.cu @@ -6,8 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "cuda_common.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -37,44 +35,32 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; // Allocate the device output vector C err = cudaMalloc((void **)&deviceObj->d_C, size_c_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -87,51 +73,62 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); - cudaEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector C from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; } + + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } + if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -139,7 +136,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -149,8 +146,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.h b/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.h deleted file mode 100644 index 0a00fa4d..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./matrix_multiplication_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda.cu index fcb0b93b..e0636728 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda.cu @@ -26,17 +26,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_lib.cu index 1a4ba80a..c00d640a 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_lib.cu @@ -1,10 +1,12 @@ #include #include "../benchmark_library.h" - - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ GraficObject* deviceObj = static_cast(device_object); + + // kernel time execution + Clock kernelCLK; + // cublas settings int lda=m,ldb=m,ldc=m; const bench_t alf = 1; @@ -14,11 +16,10 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, cublasHandle_t handle; cublasCreate(&handle); - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + //cublasSetMathMode(handle, CUBLAS_TENSOR_OP_MATH); #ifdef INT printf("CUBLAS NOT SUPPORT INT OPERATIOS\n"); @@ -27,13 +28,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, #else cublasDgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, n, w, alpha, deviceObj->d_B, lda, deviceObj->d_A, ldb, beta, deviceObj->d_C, ldc); #endif - + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cublas cublasDestroy(handle); diff --git a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_opt.cu index da81a5ab..be4cecb2 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench/cuda/lib_cuda_opt.cu @@ -65,17 +65,19 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.cpp b/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.cpp index 7293b00d..76121281 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" @@ -39,44 +38,38 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; // Allocate the device output vector C err = hipMalloc((void **)&deviceObj->d_C, size_c_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -89,52 +82,61 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector C from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -142,7 +144,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -152,9 +154,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -162,17 +163,16 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->d_C); - if (err != hipSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); + fprintf(stderr, "Failed to free device vector C (error code %s)!\n", hipGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.h b/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.h deleted file mode 100644 index 979283f7..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./matrix_multiplication_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip.cpp b/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip.cpp index 56af49f2..98bfcdc0 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -28,17 +27,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((matrix_multiplication_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip_opt.cpp index aca8601c..3e0c7802 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -65,18 +64,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((matrix_multiplication_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/main.cpp b/gpu4s_benchmark/matrix_multiplication_bench/main.cpp index 65287dba..96207bcf 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/main.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/main.cpp @@ -122,9 +122,17 @@ int main(int argc, char *argv[]) if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + matrix_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + matrix_bench->profiling_clock = true; + #endif // init memory - device_memory_init(matrix_bench, arguments_parameters->size * arguments_parameters->size, arguments_parameters->size * arguments_parameters->size, size_matrix); + device_memory_init(matrix_bench, size_matrix, size_matrix, size_matrix); #ifdef UNIFIED_MEMORY bench_t *A, *B, *C; @@ -174,8 +182,6 @@ int main(int argc, char *argv[]) } - - if (arguments_parameters->verification) { Clock cpuKernelCLK; @@ -239,7 +245,6 @@ return 0; // Arguments part - void print_usage(const char * appName) { printf("Usage: %s -s Size [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); @@ -255,6 +260,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -268,6 +274,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; // --- Properly clear character arrays --- arguments_parameters->input_file_A[0] = '\0'; arguments_parameters->input_file_B[0] = '\0'; @@ -292,7 +299,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par case 't' : arguments_parameters->print_timing = true;break; case 'c' : arguments_parameters->csv_format = true;break; case 'C' : arguments_parameters->csv_format_timestamp = true;break; - case 'g' : arguments_parameters->export_results_gpu = true;break; + case 'g' : arguments_parameters->export_results_gpu = true;break; case 'd' : args +=1; arguments_parameters->gpu = atoi(argv[args]);break; case 'f' : arguments_parameters->mute_messages = true;break; args +=1; @@ -303,7 +310,8 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par args +=1; strcpy(arguments_parameters->input_file_B,argv[args]); //TODO FIX with final version of input files break; - case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -317,4 +325,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} +} \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/benchmark_library.h b/gpu4s_benchmark/matrix_multiplication_bench/opencl/benchmark_library.h deleted file mode 100644 index 750d4f13..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/benchmark_library.h +++ /dev/null @@ -1,63 +0,0 @@ -/** * ==================================================================== - * @file benchmark_library.h (./fast_fourier_transform_window_bench) - * @brief Specific memory structures and function overloads - * for the Fast Fourier Transform Window benchmark. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once -// Include all the benchmark common variable, struct, prototype, lib -#include "benchmark_common.h" - -// ======= Benchmark local variable ======= -// --- CUDA Data types --- -#ifdef CUDA - // CUDA lib - #include - #ifdef FLOAT - typedef cufftComplex bench_cuda_complex; - #elif DOUBLE - typedef cufftDoubleComplex bench_cuda_complex; - #endif -#endif - -struct GraficObject : public GraficCommon { - #ifdef CUDA - // CUDA PART - #ifdef LIB - bench_cuda_complex* d_A; - bench_cuda_complex* d_B; - #else - bench_t* d_A; - bench_t* d_B; - #endif - #elif OPENCL - // OpenCL PART - cl::Event *evt_copyB; - cl::Event *evt_copyBr; - cl::Event *evt; - cl::Event *evt_end; - cl::Buffer *d_A; - cl::Buffer *d_B; - #elif HIP - // Hip part - bench_t* d_A; - bench_t* d_B; - #elif OPENMP - bench_t* d_A; - bench_t* d_B; - bench_t* d_Br; - #else - //CPU PART - bench_t* d_A; - bench_t* d_B; - bench_t* d_Br; - #endif -}; - - -// --- Specefic overload of benchmarking function --- -bool device_memory_init(GraficCommon* device_object, int64_t size_a_array, int64_t size_b_array); -void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A,int64_t size); -void execute_kernel(GraficCommon* device_object,int64_t window, int64_t n); -void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int64_t size); diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl.cpp index 8e0477ce..6c75c3a3 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl.cpp @@ -8,6 +8,9 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local= BLOCK_SIZE; const unsigned int y_local= BLOCK_SIZE; + // kernel time execution + Clock kernelCLK; + cl::NDRange local(x_local, y_local); cl::NDRange global(n, w); @@ -23,10 +26,9 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // Clock profilling start + kernelCLK.start(); + cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); kernel_add.setArg(0,*deviceObj->d_A); @@ -38,10 +40,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_lib.cpp index 878b3f1d..86dd0521 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_lib.cpp @@ -11,21 +11,26 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const unsigned int a_ld = n; const unsigned int b_ld = n; const unsigned int c_ld = n; + // kernel time execution + Clock kernelCLK; + + + #ifdef INT - printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); + printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); #else - #ifdef PROFILING_CLOCK - // Ensure queue is idle before measuring - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - + // Clock profilling start + kernelCLK.start(); + auto status = clblast::Gemm(clblast::Layout::kRowMajor,clblast::Transpose::kNo, clblast::Transpose::kNo, n, n, n, alpha, (*deviceObj->d_A)() , 0, a_ld, (*deviceObj->d_B)(), 0, b_ld, beta, (*deviceObj->d_C)(), 0, c_ld,&(*deviceObj->queue)(), &(*deviceObj->evt)()); // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); #endif } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_opt.cpp index cac31afc..e7bb19f7 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/opencl/lib_opencl_opt.cpp @@ -8,6 +8,9 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local= BLOCK_SIZE; const unsigned int y_local= BLOCK_SIZE; + // kernel time execution + Clock kernelCLK; + cl::NDRange local(x_local, y_local); cl::NDRange global(n, w); @@ -20,10 +23,9 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); kernel_add.setArg(0,*deviceObj->d_A); @@ -38,8 +40,10 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.cpp index 558d2b5f..babb3f56 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.cpp @@ -9,8 +9,6 @@ #include "../cpu_functions/cpu_functions.h" #include - - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -70,11 +68,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; - // copy memory host -> device + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -91,10 +89,11 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } #ifdef UNIFIED_MEMORY @@ -157,16 +156,24 @@ void device_unified_memory_init_copy(GraficCommon* device_object, bench_t* &A, b void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling start + d2hCLK.start(); + + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector C from device to host (OpenCL error code %d)!\n", err); + return; + } + + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } #ifdef UNIFIED_MEMORY @@ -185,30 +192,27 @@ void copy_memory_unified_to_host(GraficCommon* device_object, bench_t* &d_C, uns float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - deviceObj->evt_copyC->wait(); + deviceObj->evt_copyC->wait(); // wait + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - // --- select the profiling message --- - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif - - #ifdef UNIFIED_MEMORY - elapsed_h_d = h2dTotal; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0, current_time); @@ -216,7 +220,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.h b/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.h deleted file mode 100644 index 15b04ce5..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench/opencl/opencl_common.h +++ /dev/null @@ -1,21 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./matrix_multiplication_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; - - #ifdef UNIFIED_MEMORY - float h2dTotal = 0; - #endif -#endif diff --git a/gpu4s_benchmark/matrix_multiplication_bench/openmp/omp_common.h b/gpu4s_benchmark/matrix_multiplication_bench/openmp/omp_common.h deleted file mode 100644 index a740fb58..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench/openmp/omp_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file omp_common.h (./matrix_multiplication_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenMP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cpu_functions/cpu_functions.h b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cpu_functions/cpu_functions.h index af8487dc..bf097b4c 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cpu_functions/cpu_functions.h @@ -35,6 +35,23 @@ union } binary_float; #endif +struct BenchmarkParameters{ + int size = 0; + unsigned int gpu = 0; + bool verification = false; + bool export_results = false; + bool export_results_gpu = false; + bool print_output = false; + bool print_timing = false; + bool csv_format = false; + bool mute_messages = false; + bool csv_format_timestamp = false; + char input_file_A[100] = ""; + char input_file_B[100] = ""; + char output_file[100] = ""; + bool profiling_clock = false; +}; + void matrix_multiplication(const bench_t* A, const bench_t* B, bench_t* C,const unsigned int n, const unsigned int m, const unsigned int w ); //bool compare_vectors_int(const int* host,const int* device,const int size); //bool compare_vectors(const float* host,const float* device, const int size); diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.cu b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.cu index c3ad8460..aa915ae0 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.cu @@ -53,7 +53,7 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ -GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); // Allocate the device input vector A cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); @@ -86,12 +86,13 @@ GraficObject* deviceObj = static_cast(device_object); void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -115,22 +116,23 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h convert_fp32_to_f16<<>> (deviceObj->d_B, deviceObj->d_half_B, size_b); #endif + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } // --- FLOAT16 copy back does not work with opt --- __attribute__((weak)) void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); #ifdef FLOAT16 @@ -139,39 +141,45 @@ void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ convert_fp16_to_f32<<>> (deviceObj->d_half_C, deviceObj->d_C, size); #endif - cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + // profilling end + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector C from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -181,9 +189,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format){ void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -191,17 +198,16 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->d_C); + err = cudaFree(deviceObj->d_C); if (err != cudaSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); + fprintf(stderr, "Failed to free device vector C (error code %s)!\n", cudaGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.h b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.h deleted file mode 100644 index 4b5f108a..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/cuda_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./matrix_multiplication_bench_fp16) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda.cu b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda.cu index de487f93..d4c81486 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda.cu @@ -7,8 +7,6 @@ * number of elements numElements. */ - - __global__ void matrix_multiplication_kernel(const bench_t_gpu *A,const bench_t_gpu *B, bench_t_gpu *C, const int n, const int m, const int w) { @@ -28,11 +26,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); #ifdef FLOAT16 @@ -41,10 +39,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); #endif - cudaEventRecord(*deviceObj->stop); + // profilling end + cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_lib.cu index aaaea1c5..edbe4b51 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_lib.cu @@ -11,13 +11,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const bench_t_gpu *beta = &bet; cublasHandle_t handle; cublasCreate(&handle); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); cublasSetMathMode(handle, CUBLAS_TENSOR_OP_MATH); + #ifdef INT printf("CUBLAS NOT SUPPORT INT OPERATIOS\n"); #elif FLOAT16 @@ -28,12 +29,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, cublasDgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, n, w, alpha, deviceObj->d_B, lda, deviceObj->d_A, ldb, beta, deviceObj->d_C, ldc); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cublas cublasDestroy(handle); diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_opt.cu index aa9a8b16..adc49316 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/cuda/lib_cuda_opt.cu @@ -124,11 +124,11 @@ matrix_multiplication_kernel(const bench_t *A, const bench_t *B, bench_t *C, con void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); #ifdef FLOAT16 @@ -142,27 +142,36 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector C from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/main.cpp b/gpu4s_benchmark/matrix_multiplication_bench_fp16/main.cpp index d0473d8c..d98e76ef 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/main.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/main.cpp @@ -13,7 +13,7 @@ #define GPU_FILE "gpu_file.out" #define CPU_FILE "cpu_file.out" -int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,char *input_file, char *output_file); +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters); int main(int argc, char *argv[]){ // random init @@ -21,12 +21,10 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // Arguments /////////////////////////////////////////////////////////////////////////////////////////////// - unsigned int size = 0, gpu = 0; - bool verification = false, export_results = false, print_output = false, print_timing = false, export_results_gpu = false, csv_format = false; - char input_file[100] = ""; - char output_file[100] = ""; + BenchmarkParameters *arguments_parameters = (BenchmarkParameters *)malloc(sizeof(BenchmarkParameters)); - int resolution = arguments_handler(argc,argv, &size, &gpu, &verification, &export_results, &export_results_gpu,&print_output, &print_timing, &csv_format,input_file, output_file); + + int resolution = arguments_handler(argc,argv,arguments_parameters); if (resolution == ERROR_ARGUMENTS){ exit(-1); } @@ -34,17 +32,17 @@ int main(int argc, char *argv[]){ // VARIABLES /////////////////////////////////////////////////////////////////////////////////////////////// // linearizable versions of matrix - unsigned int size_matrix =size * size; + unsigned int size_matrix = arguments_parameters->size * arguments_parameters->size; // A input matrix - unsigned int size_A = size * size; + unsigned int size_A = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_A = sizeof(bench_t) * size_A; bench_t* A = (bench_t*) malloc(mem_size_A); // B input matrix - unsigned int size_B = size * size; + unsigned int size_B = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_B = sizeof(bench_t) * size_B; bench_t* B = (bench_t*) malloc(mem_size_B); // C matrix - unsigned int size_C = size * size; + unsigned int size_C = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_C = sizeof(bench_t) * size_C; bench_t* h_C = (bench_t*) malloc(mem_size_C); bench_t* d_C = (bench_t*) malloc(mem_size_C); @@ -55,50 +53,50 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // DATA INIT /////////////////////////////////////////////////////////////////////////////////////////////// - if (strlen(input_file) == 0) + if (strlen(arguments_parameters->input_file_A) == 0) { // inicialice A matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ #ifdef INT - A[i*size+j] = rand() % (NUMBER_BASE * 100); + A[i*arguments_parameters->size+j] = rand() % (NUMBER_BASE * 100); #else - A[i*size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); + A[i*arguments_parameters->size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); #endif } } // iniciate B matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ #ifdef INT - B[i*size+j] = rand() % (NUMBER_BASE * 100); + B[i*arguments_parameters->size+j] = rand() % (NUMBER_BASE * 100); #else - B[i*size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); + B[i*arguments_parameters->size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); #endif } } // iniciate C matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + h_C[i*arguments_parameters->size+j] = 0; + d_C[i*arguments_parameters->size+j] = 0; } } } else { - // load data - //get_double_hexadecimal_values(input_file_A, A,size_A); - //get_double_hexadecimal_values(input_file_B, B,size_B); + /// load data + get_double_hexadecimal_values(arguments_parameters->input_file_A, A,size_A); + get_double_hexadecimal_values(arguments_parameters->input_file_B, B,size_B); //get_values_file(input_file, A, B); // iniciate C matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + h_C[i*arguments_parameters->size+j] = 0; + d_C[i*arguments_parameters->size+j] = 0; } } @@ -112,39 +110,48 @@ int main(int argc, char *argv[]){ GraficCommon*matrix_benck = (GraficCommon*)malloc(sizeof(GraficObject)); // init devices char device[100] = ""; - init(matrix_benck, 0,gpu, device); - if (!csv_format){ + init(matrix_benck, 0,arguments_parameters->gpu, device); + if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + matrix_benck->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + matrix_benck->profiling_clock = true; + #endif + // init memory - device_memory_init(matrix_benck, size * size, size * size, size_matrix); + device_memory_init(matrix_benck, size_matrix, size_matrix, size_matrix); // copy memory to device - copy_memory_to_device(matrix_benck, A, B, size * size, size * size); + copy_memory_to_device(matrix_benck, A, B, size_matrix, size_matrix); // execute kernel - execute_kernel(matrix_benck, size, size, size); + execute_kernel(matrix_benck, arguments_parameters->size, arguments_parameters->size, arguments_parameters->size); // copy memory to host copy_memory_to_host(matrix_benck, d_C, size_matrix); // get time - if (print_timing || csv_format) + if (arguments_parameters->print_timing || arguments_parameters->csv_format || arguments_parameters->csv_format_timestamp) { - get_elapsed_time(matrix_benck, csv_format); + get_elapsed_time(matrix_benck, arguments_parameters->csv_format); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%d ", d_C[i*arguments_parameters->size+j]); } printf("\n"); } #else - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%f ", d_C[i*arguments_parameters->size+j]); } printf("\n"); @@ -156,30 +163,30 @@ int main(int argc, char *argv[]){ - if (verification) + if (arguments_parameters->verification) { Clock cpuKernelCLK; cpuKernelCLK.start(); - matrix_multiplication(A, B, h_C, size, size, size); + matrix_multiplication(A, B, h_C, arguments_parameters->size, arguments_parameters->size, arguments_parameters->size); cpuKernelCLK.end(); - if (print_timing) + if (arguments_parameters->print_timing) { printf("CPU Time %.0f milliseconds\n", cpuKernelCLK.getElapsedMS()); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%d ", h_C[i*arguments_parameters->size+j]); } printf("\n"); } #else - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%f ", h_C[i*arguments_parameters->size+j]); } printf("\n"); @@ -190,16 +197,16 @@ int main(int argc, char *argv[]){ if (result){ printf("OK\n"); } - if (export_results){ + if (arguments_parameters->export_results){ //set_values_file(output_file, d_C, size); - //print_double_hexadecimal_values(GPU_FILE, d_C, size_C); - //print_double_hexadecimal_values(CPU_FILE, h_C, size_C); + print_double_hexadecimal_values(GPU_FILE, d_C, size_C); + print_double_hexadecimal_values(CPU_FILE, h_C, size_C); } } - if (export_results_gpu) + if (arguments_parameters->export_results_gpu) { - //print_double_hexadecimal_values(GPU_FILE, d_C, size_C); + print_double_hexadecimal_values(GPU_FILE, d_C, size_C); //set_values_file(output_file, d_C, size); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -207,6 +214,7 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // clean device memory clean(matrix_benck); + free(arguments_parameters); // free object memory free(matrix_benck); free(A); @@ -218,7 +226,6 @@ return 0; // Arguments part - void print_usage(const char * appName) { printf("Usage: %s -s Size [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); @@ -229,13 +236,35 @@ void print_usage(const char * appName) printf(" -o: prints the results\n"); printf(" -t: prints the timing\n"); printf(" -c: prints the timing in csv format\n"); + printf(" -C: prints the timing in csv format with timestamp\n"); printf(" -i: pass input data and the result and compares\n"); printf(" -d: selects GPU\n"); + printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } +void init_arguments(BenchmarkParameters* arguments_parameters){ + arguments_parameters->size = 0; + arguments_parameters->gpu = 0; + arguments_parameters->verification = false; + arguments_parameters->export_results = false; + arguments_parameters->export_results_gpu = false; + arguments_parameters->print_output = false; + arguments_parameters->print_timing = false; + arguments_parameters->csv_format = false; + arguments_parameters->mute_messages = false; + arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; + // --- Properly clear character arrays --- + arguments_parameters->input_file_A[0] = '\0'; + arguments_parameters->input_file_B[0] = '\0'; + arguments_parameters->output_file[0] = '\0'; +} -int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,char *input_file_A, char *input_file_B){ + +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ + init_arguments(arguments_parameters); if (argc == 1){ printf("-s need to be set\n\n"); print_usage(argv[0]); @@ -245,28 +274,36 @@ int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *g { switch (argv[args][1]) { // comon part - case 'v' : *verification = true;break; - case 'e' : *verification = true; *export_results= true;break; - case 'o' : *print_output = true;break; - case 't' : *print_timing = true;break; - case 'c' : *csv_format = true;break; - case 'g' : *export_results_gpu = true;break; - case 'd' : args +=1; *gpu = atoi(argv[args]);break; - // specific - case 'i' : args +=1; - strcpy(input_file_A,argv[args]); + case 'v' : arguments_parameters->verification = true;break; + case 'e' : arguments_parameters->verification = true; arguments_parameters->export_results= true;break; + case 'o' : arguments_parameters->print_output = true;break; + case 't' : arguments_parameters->print_timing = true;break; + case 'c' : arguments_parameters->csv_format = true;break; + case 'C' : arguments_parameters->csv_format_timestamp = true;break; + case 'g' : arguments_parameters->export_results_gpu = true;break; + case 'd' : args +=1; arguments_parameters->gpu = atoi(argv[args]);break; + case 'f' : arguments_parameters->mute_messages = true;break; args +=1; - strcpy(input_file_B,argv[args]); + strcpy(arguments_parameters->output_file,argv[args]); break; - case 's' : args +=1; *size = atoi(argv[args]);break; + case 'i' : args +=1; + strcpy(arguments_parameters->input_file_A,argv[args]); + args +=1; + strcpy(arguments_parameters->input_file_B,argv[args]); //TODO FIX with final version of input files + break; + case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } } - if ( *size <= 0){ + if ( arguments_parameters->size <= 0){ printf("-s need to be set\n\n"); print_usage(argv[0]); return ERROR_ARGUMENTS; } + if (arguments_parameters->mute_messages){ + arguments_parameters->csv_format = false; + } return OK_ARGUMENTS; -} +} \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl.cpp b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl.cpp index 3dd068d9..ad520d34 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl.cpp @@ -4,7 +4,6 @@ #include #include "kernel.cl" - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int w){ GraficObject* deviceObj = static_cast(device_object); const unsigned int x_local= BLOCK_SIZE; @@ -23,6 +22,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); #ifdef FLOAT16 kernel_add.setArg(0,*deviceObj->d_half_A); @@ -37,15 +43,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(4,m); kernel_add.setArg(5,w); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_lib.cpp index 068b8dfd..f509c6c4 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_lib.cpp @@ -3,7 +3,6 @@ #include "../benchmark_library.h" #include - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int w){ GraficObject* deviceObj = static_cast(device_object); const bench_t alpha = 1.0f; @@ -11,38 +10,50 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const unsigned int a_ld = n; const unsigned int b_ld = n; const unsigned int c_ld = n; + #ifdef INT - printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); + printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); #else - #ifdef PROFILING_CLOCK - // Ensure queue is idle before measuring - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + // Ensure queue is idle before measuring + deviceObj->queue->finish(); + kernelCLK.start(); auto status = clblast::Gemm(clblast::Layout::kRowMajor,clblast::Transpose::kNo, clblast::Transpose::kNo, n, n, n, alpha, (*deviceObj->d_A)() , 0, a_ld, (*deviceObj->d_B)(), 0, b_ld, beta, (*deviceObj->d_C)(), 0, c_ld,&(*deviceObj->queue)(), &(*deviceObj->evt)()); // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); #endif } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); - + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); + + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector C from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_opt.cpp index 22c7d1b4..46624148 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/lib_opencl_opt.cpp @@ -21,6 +21,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + + cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); #ifdef FLOAT16 kernel_add.setArg(0,*deviceObj->d_half_A); @@ -36,15 +44,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(5,w); kernel_add.setArg(6,BLOCK_SIZE); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.cpp b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.cpp index 6aa62d20..2b31f053 100644 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.cpp @@ -96,9 +96,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // Clock profilling start + h2dCLK.start(); // copy memory host -> device @@ -128,19 +130,22 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h deviceObj->queue->enqueueNDRangeKernel(kernel_fp32_to_fp16, cl::NullRange, cl::NDRange(size_b), cl::NullRange); #endif - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } // --- FLOAT16 copy back does not work with LIB --- __attribute__((weak)) void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); #ifdef FLOAT16 // --- Convert back to FP32 --- @@ -149,44 +154,48 @@ void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ deviceObj->queue->enqueueNDRangeKernel(kernel_fp16_to_fp32, cl::NullRange, cl::NDRange(size), cl::NullRange); #endif - - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); - + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector C from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the d2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.h b/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.h deleted file mode 100644 index 71282887..00000000 --- a/gpu4s_benchmark/matrix_multiplication_bench_fp16/opencl/opencl_common.h +++ /dev/null @@ -1,18 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./matrix_multiplication_bench_fp16) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cpu_functions/cpu_functions.h index 3b94ec36..f121fed1 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cpu_functions/cpu_functions.h @@ -38,6 +38,23 @@ union } binary_float; #endif +struct BenchmarkParameters{ + int size = 0; + unsigned int gpu = 0; + bool verification = false; + bool export_results = false; + bool export_results_gpu = false; + bool print_output = false; + bool print_timing = false; + bool csv_format = false; + bool mute_messages = false; + bool csv_format_timestamp = false; + char input_file_A[100] = ""; + char input_file_B[100] = ""; + char output_file[100] = ""; + bool profiling_clock = false; +}; + void matrix_multiplication(const bench_t* A, const bench_t* B, bench_t* C,const unsigned int n, const unsigned int m, const unsigned int w ); //bool compare_vectors_int(const int* host,const int* device,const int size); //bool compare_vectors(const float* host,const float* device, const int size); diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.cu b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.cu index fcc67fe6..2e470956 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.cu @@ -7,7 +7,6 @@ * ======================================================================= */ #include "../benchmark_library.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -36,44 +35,34 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } // Allocate the device output vector C err = cudaMalloc((void **)&deviceObj->d_C, size_c_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -86,55 +75,64 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h fprintf(stderr, "Failed to copy vector B from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_C, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector C from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -144,9 +142,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format){ void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -154,17 +151,16 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } - err = cudaFree(deviceObj->d_C); + err = cudaFree(deviceObj->d_C); if (err != cudaSuccess) { - fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); + fprintf(stderr, "Failed to free device vector C (error code %s)!\n", cudaGetErrorString(err)); return; } diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.h b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.h deleted file mode 100644 index e474ffdf..00000000 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./matrix_multiplication_tensor_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda.cu index fbcd54cb..54744b3b 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda.cu @@ -6,8 +6,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 16 -__global__ void + + __global__ void matrix_multiplication_kernel(const bench_t *A,const bench_t *B, bench_t *C, const int n, const int m, const int w) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -26,18 +26,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_lib.cu index 05670a72..647fe6ac 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_lib.cu @@ -11,12 +11,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const bench_t *beta = &bet; cublasHandle_t handle; cublasCreate(&handle); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + //cublasSetMathMode(handle, CUBLAS_TENSOR_OP_MATH); #ifdef INT printf("CUBLAS NOT SUPPORT INT OPERATIOS\n"); @@ -26,12 +27,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, cublasDgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, m, n, w, alpha, deviceObj->d_B, lda, deviceObj->d_A, ldb, beta, deviceObj->d_C, ldc); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cublas cublasDestroy(handle); diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_opt.cu index 6b93537d..159b4976 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/cuda/lib_cuda_opt.cu @@ -6,8 +6,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 16 -__global__ void + + __global__ void matrix_multiplication_kernel(const bench_t *A,const bench_t *B, bench_t *C, const int n, const int m, const int w) { __shared__ bench_t A_tile[BLOCK_SIZE*BLOCK_SIZE]; @@ -63,18 +63,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + matrix_multiplication_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->d_C, n, m, w); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/main.cpp b/gpu4s_benchmark/matrix_multiplication_tensor_bench/main.cpp index cd285daf..6c4b4c2c 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/main.cpp +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/main.cpp @@ -13,7 +13,7 @@ #define GPU_FILE "gpu_file.out" #define CPU_FILE "cpu_file.out" -int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,char *input_file, char *output_file); +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters); int main(int argc, char *argv[]){ // random init @@ -21,12 +21,10 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // Arguments /////////////////////////////////////////////////////////////////////////////////////////////// - unsigned int size = 0, gpu = 0; - bool verification = false, export_results = false, print_output = false, print_timing = false, export_results_gpu = false, csv_format = false; - char input_file[100] = ""; - char output_file[100] = ""; + BenchmarkParameters *arguments_parameters = (BenchmarkParameters *)malloc(sizeof(BenchmarkParameters)); - int resolution = arguments_handler(argc,argv, &size, &gpu, &verification, &export_results, &export_results_gpu,&print_output, &print_timing, &csv_format,input_file, output_file); + + int resolution = arguments_handler(argc,argv,arguments_parameters); if (resolution == ERROR_ARGUMENTS){ exit(-1); } @@ -34,17 +32,17 @@ int main(int argc, char *argv[]){ // VARIABLES /////////////////////////////////////////////////////////////////////////////////////////////// // linearizable versions of matrix - unsigned int size_matrix =size * size; + unsigned int size_matrix = arguments_parameters->size * arguments_parameters->size; // A input matrix - unsigned int size_A = size * size; + unsigned int size_A = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_A = sizeof(bench_t) * size_A; bench_t* A = (bench_t*) malloc(mem_size_A); // B input matrix - unsigned int size_B = size * size; + unsigned int size_B = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_B = sizeof(bench_t) * size_B; bench_t* B = (bench_t*) malloc(mem_size_B); // C matrix - unsigned int size_C = size * size; + unsigned int size_C = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_C = sizeof(bench_t) * size_C; bench_t* h_C = (bench_t*) malloc(mem_size_C); bench_t* d_C = (bench_t*) malloc(mem_size_C); @@ -55,50 +53,50 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // DATA INIT /////////////////////////////////////////////////////////////////////////////////////////////// - if (strlen(input_file) == 0) + if (strlen(arguments_parameters->input_file_A) == 0) { // inicialice A matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ #ifdef INT - A[i*size+j] = rand() % (NUMBER_BASE * 100); + A[i*arguments_parameters->size+j] = rand() % (NUMBER_BASE * 100); #else - A[i*size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); + A[i*arguments_parameters->size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); #endif } } // iniciate B matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ #ifdef INT - B[i*size+j] = rand() % (NUMBER_BASE * 100); + B[i*arguments_parameters->size+j] = rand() % (NUMBER_BASE * 100); #else - B[i*size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); + B[i*arguments_parameters->size+j] = (bench_t)rand()/(bench_t)(RAND_MAX/NUMBER_BASE); #endif } } // iniciate C matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + h_C[i*arguments_parameters->size+j] = 0; + d_C[i*arguments_parameters->size+j] = 0; } } } else { - // load data - //get_double_hexadecimal_values(input_file_A, A,size_A); - //get_double_hexadecimal_values(input_file_B, B,size_B); + /// load data + get_double_hexadecimal_values(arguments_parameters->input_file_A, A,size_A); + get_double_hexadecimal_values(arguments_parameters->input_file_B, B,size_B); //get_values_file(input_file, A, B); // iniciate C matrix - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + h_C[i*arguments_parameters->size+j] = 0; + d_C[i*arguments_parameters->size+j] = 0; } } @@ -112,39 +110,47 @@ int main(int argc, char *argv[]){ GraficCommon*matrix_benck = (GraficCommon*)malloc(sizeof(GraficObject)); // init devices char device[100] = ""; - init(matrix_benck, 0,gpu, device); - if (!csv_format){ + init(matrix_benck, 0, arguments_parameters->gpu, device); + if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + // Update profiling clock mode + matrix_benck->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + matrix_benck->profiling_clock = true; + #endif + // init memory - device_memory_init(matrix_benck, size * size, size * size, size_matrix); + device_memory_init(matrix_benck, size_matrix, size_matrix, size_matrix); // copy memory to device - copy_memory_to_device(matrix_benck, A, B, size * size, size * size); + copy_memory_to_device(matrix_benck, A, B, size_matrix, size_matrix); // execute kernel - execute_kernel(matrix_benck, size, size, size); + execute_kernel(matrix_benck, arguments_parameters->size, arguments_parameters->size, arguments_parameters->size); // copy memory to host copy_memory_to_host(matrix_benck, d_C, size_matrix); // get time - if (print_timing || csv_format) + if (arguments_parameters->print_timing || arguments_parameters->csv_format || arguments_parameters->csv_format_timestamp) { - get_elapsed_time(matrix_benck, csv_format); + get_elapsed_time(matrix_benck, arguments_parameters->csv_format); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%d ", d_C[i*arguments_parameters->size+j]); } printf("\n"); } #else - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%f ", d_C[i*arguments_parameters->size+j]); } printf("\n"); @@ -155,31 +161,30 @@ int main(int argc, char *argv[]){ } - - if (verification) + if (arguments_parameters->verification) { Clock kernelCLK; kernelCLK.start(); - matrix_multiplication(A, B, h_C, size, size, size); + matrix_multiplication(A, B, h_C, arguments_parameters->size, arguments_parameters->size, arguments_parameters->size); kernelCLK.end(); - if (print_timing) + if (arguments_parameters->print_timing) { printf("CPU Time %.0f milliseconds\n", kernelCLK.getElapsedMS() ); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%d ", h_C[i*arguments_parameters->size+j]); } printf("\n"); } #else - for (int i=0; isize; i++){ + for (int j=0; jsize; j++){ + printf("%f ", h_C[i*arguments_parameters->size+j]); } printf("\n"); @@ -190,16 +195,15 @@ int main(int argc, char *argv[]){ if (result){ printf("OK\n"); } - if (export_results){ + if (arguments_parameters->export_results){ //set_values_file(output_file, d_C, size); - //print_double_hexadecimal_values(GPU_FILE, d_C, size_C); - //print_double_hexadecimal_values(CPU_FILE, h_C, size_C); + print_double_hexadecimal_values(GPU_FILE, d_C, size_C); + print_double_hexadecimal_values(CPU_FILE, h_C, size_C); } - } - if (export_results_gpu) + if (arguments_parameters->export_results_gpu) { - //print_double_hexadecimal_values(GPU_FILE, d_C, size_C); + print_double_hexadecimal_values(GPU_FILE, d_C, size_C); //set_values_file(output_file, d_C, size); } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -207,6 +211,7 @@ int main(int argc, char *argv[]){ /////////////////////////////////////////////////////////////////////////////////////////////// // clean device memory clean(matrix_benck); + free(arguments_parameters); // free object memory free(matrix_benck); free(A); @@ -218,7 +223,7 @@ return 0; // Arguments part - +// Arguments part void print_usage(const char * appName) { printf("Usage: %s -s Size [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); @@ -229,13 +234,35 @@ void print_usage(const char * appName) printf(" -o: prints the results\n"); printf(" -t: prints the timing\n"); printf(" -c: prints the timing in csv format\n"); + printf(" -C: prints the timing in csv format with timestamp\n"); printf(" -i: pass input data and the result and compares\n"); printf(" -d: selects GPU\n"); + printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); +} + +void init_arguments(BenchmarkParameters* arguments_parameters){ + arguments_parameters->size = 0; + arguments_parameters->gpu = 0; + arguments_parameters->verification = false; + arguments_parameters->export_results = false; + arguments_parameters->export_results_gpu = false; + arguments_parameters->print_output = false; + arguments_parameters->print_timing = false; + arguments_parameters->csv_format = false; + arguments_parameters->mute_messages = false; + arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; + // --- Properly clear character arrays --- + arguments_parameters->input_file_A[0] = '\0'; + arguments_parameters->input_file_B[0] = '\0'; + arguments_parameters->output_file[0] = '\0'; } -int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,char *input_file_A, char *input_file_B){ +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ + init_arguments(arguments_parameters); if (argc == 1){ printf("-s need to be set\n\n"); print_usage(argv[0]); @@ -245,28 +272,36 @@ int arguments_handler(int argc, char ** argv,unsigned int *size, unsigned int *g { switch (argv[args][1]) { // comon part - case 'v' : *verification = true;break; - case 'e' : *verification = true; *export_results= true;break; - case 'o' : *print_output = true;break; - case 't' : *print_timing = true;break; - case 'c' : *csv_format = true;break; - case 'g' : *export_results_gpu = true;break; - case 'd' : args +=1; *gpu = atoi(argv[args]);break; - // specific - case 'i' : args +=1; - strcpy(input_file_A,argv[args]); + case 'v' : arguments_parameters->verification = true;break; + case 'e' : arguments_parameters->verification = true; arguments_parameters->export_results= true;break; + case 'o' : arguments_parameters->print_output = true;break; + case 't' : arguments_parameters->print_timing = true;break; + case 'c' : arguments_parameters->csv_format = true;break; + case 'C' : arguments_parameters->csv_format_timestamp = true;break; + case 'g' : arguments_parameters->export_results_gpu = true;break; + case 'd' : args +=1; arguments_parameters->gpu = atoi(argv[args]);break; + case 'f' : arguments_parameters->mute_messages = true;break; args +=1; - strcpy(input_file_B,argv[args]); + strcpy(arguments_parameters->output_file,argv[args]); break; - case 's' : args +=1; *size = atoi(argv[args]);break; + case 'i' : args +=1; + strcpy(arguments_parameters->input_file_A,argv[args]); + args +=1; + strcpy(arguments_parameters->input_file_B,argv[args]); //TODO FIX with final version of input files + break; + case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } } - if ( *size <= 0){ + if ( arguments_parameters->size <= 0){ printf("-s need to be set\n\n"); print_usage(argv[0]); return ERROR_ARGUMENTS; } + if (arguments_parameters->mute_messages){ + arguments_parameters->csv_format = false; + } return OK_ARGUMENTS; -} +} \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl.cpp index 8a73b17a..6930219a 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl.cpp @@ -22,6 +22,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); kernel_add.setArg(0,*deviceObj->d_A); kernel_add.setArg(1,*deviceObj->d_B); @@ -30,16 +37,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(4,m); kernel_add.setArg(5,w); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_lib.cpp index 5ee6aeaa..baf16020 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_lib.cpp @@ -10,22 +10,27 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, const unsigned int a_ld = n; const unsigned int b_ld = n; const unsigned int c_ld = n; + #ifdef INT - printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); + printf("CLBLAST NOT SUPPORT INT OPERATIOS\n"); #else - #ifdef PROFILING_CLOCK - // Ensure queue is idle before measuring - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + deviceObj->queue->finish(); + kernelCLK.start(); + auto status = clblast::Gemm(clblast::Layout::kRowMajor,clblast::Transpose::kNo, clblast::Transpose::kNo, n, n, n, alpha, (*deviceObj->d_A)() , 0, a_ld, (*deviceObj->d_B)(), 0, b_ld, beta, (*deviceObj->d_C)(), 0, c_ld,&(*deviceObj->queue)(), &(*deviceObj->evt)()); // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); #endif } diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_opt.cpp index f106d145..20615e9d 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/lib_opencl_opt.cpp @@ -21,6 +21,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + cl::Kernel kernel_add=cl::Kernel(program,"kernel_matrix_multiplication"); kernel_add.setArg(0,*deviceObj->d_A); kernel_add.setArg(1,*deviceObj->d_B); @@ -30,15 +37,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(5,w); kernel_add.setArg(6,BLOCK_SIZE); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + // Wait for completion before stopping the clock deviceObj->queue->finish(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.cpp index 6b44bd0e..b107487f 100644 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.cpp @@ -50,6 +50,7 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix, unsigned int size_c_matrix){ GraficObject* deviceObj = static_cast(device_object); cl_int err; + deviceObj->d_A = new cl::Buffer(*deviceObj->context, CL_MEM_READ_ONLY, sizeof(bench_t)*size_a_matrix, nullptr, &err); if (err != CL_SUCCESS) return false; @@ -66,9 +67,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h_B, unsigned int size_a, unsigned int size_b){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // host -> device + Clock h2dCLK; + + // Clock profilling start + h2dCLK.start(); // copy memory host -> device @@ -87,53 +90,62 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, bench_t* h return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_C, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector C from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.h b/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.h deleted file mode 100644 index 3580baaa..00000000 --- a/gpu4s_benchmark/matrix_multiplication_tensor_bench/opencl/opencl_common.h +++ /dev/null @@ -1,18 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./matrix_multiplication_tensor_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - diff --git a/gpu4s_benchmark/max_pooling_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/max_pooling_bench/cpu_functions/cpu_functions.h index 47331766..3f529132 100644 --- a/gpu4s_benchmark/max_pooling_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/max_pooling_bench/cpu_functions/cpu_functions.h @@ -56,6 +56,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.cu b/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.cu index d6b7e238..df5c31db 100644 --- a/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.cu @@ -7,15 +7,6 @@ * ======================================================================= */ #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -47,33 +38,26 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -81,50 +65,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); - cudaEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; } + + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -132,7 +125,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -142,9 +135,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -152,7 +144,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.h b/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.h deleted file mode 100644 index 9f639fae..00000000 --- a/gpu4s_benchmark/max_pooling_bench/cuda/cuda_common.h +++ /dev/null @@ -1,8 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./max_pooling_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once diff --git a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda.cu index 2af9c0ba..5974c995 100644 --- a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda.cu @@ -37,8 +37,8 @@ max_pooling_kernel(const bench_t *A, bench_t *B, const int size, const unsigned } void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w, unsigned int stride, unsigned int lateral_stride){ - GraficObject* deviceObj = static_cast(device_object); - dim3 dimBlock, dimGrid; + GraficObject* deviceObj = static_cast(device_object); + dim3 dimBlock, dimGrid; if(lateral_stride < BLOCK_SIZE) { dimBlock = dim3(lateral_stride, lateral_stride); @@ -50,17 +50,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dimGrid = dim3(ceil(((float(n) / stride ))/dimBlock.x), ceil(((float(m) / stride ))/dimBlock.y)); } + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + max_pooling_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n, stride, lateral_stride); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_lib.cu index 804e4415..5a72eb99 100644 --- a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_lib.cu @@ -20,16 +20,16 @@ #endif void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w, unsigned int stride, unsigned int size_lateral){ - GraficObject* deviceObj = static_cast(device_object); - // CUDNN settings + GraficObject* deviceObj = static_cast(device_object); + // CUDNN settings const bench_t alf = 1; const bench_t bet = 0; cudnnHandle_t cudnn; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); checkCUDNN(cudnnCreate(&cudnn)); // create input tensor @@ -77,12 +77,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, output_descriptor, deviceObj->d_B)) + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cuDNN cudnnDestroyTensorDescriptor(input_descriptor); diff --git a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_opt.cu index dea6a555..aa136f95 100644 --- a/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/max_pooling_bench/cuda/lib_cuda_opt.cu @@ -54,19 +54,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dimGrid = dim3(ceil((lateral_stride*lateral_stride)/dimBlock.x)); } - - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + max_pooling_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n, stride, lateral_stride); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/max_pooling_bench/hip/hip_common.cpp b/gpu4s_benchmark/max_pooling_bench/hip/hip_common.cpp index 106ccba7..80a79fb3 100644 --- a/gpu4s_benchmark/max_pooling_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/max_pooling_bench/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -37,36 +36,35 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipSuccess; + err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -74,50 +72,59 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -125,7 +132,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -135,9 +142,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -145,7 +151,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/max_pooling_bench/hip/hip_common.h b/gpu4s_benchmark/max_pooling_bench/hip/hip_common.h deleted file mode 100644 index 124c433a..00000000 --- a/gpu4s_benchmark/max_pooling_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./max_pooling_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/max_pooling_bench/hip/lib_hip.cpp b/gpu4s_benchmark/max_pooling_bench/hip/lib_hip.cpp index 50eb3de7..00db57fe 100644 --- a/gpu4s_benchmark/max_pooling_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/max_pooling_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -51,18 +50,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dimGrid = dim3(ceil(((float(n) / stride ))/dimBlock.x), ceil(((float(m) / stride ))/dimBlock.y)); } + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((max_pooling_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n, stride, lateral_stride); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/max_pooling_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/max_pooling_bench/hip/lib_hip_opt.cpp index c4eec367..87ac4ab9 100644 --- a/gpu4s_benchmark/max_pooling_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/max_pooling_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -55,17 +54,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, dimGrid = dim3(ceil((lateral_stride*lateral_stride)/dimBlock.x)); } + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((max_pooling_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n, stride, lateral_stride); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/max_pooling_bench/main.cpp b/gpu4s_benchmark/max_pooling_bench/main.cpp index e82a0fff..147545a8 100644 --- a/gpu4s_benchmark/max_pooling_bench/main.cpp +++ b/gpu4s_benchmark/max_pooling_bench/main.cpp @@ -113,6 +113,15 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + + // Update profiling clock mode + max_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + max_bench->profiling_clock = true; + #endif + // init memory device_memory_init(max_bench, arguments_parameters->size * arguments_parameters->size, size_B); // copy memory to device @@ -233,6 +242,7 @@ void print_usage(const char * appName) printf(" -x: prints the timing of the validation. Only the sequential time of the application will be displayed\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -247,6 +257,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -277,6 +288,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; case 'l' : args +=1; arguments_parameters->stride = atoi(argv[args]);break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -296,4 +308,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl.cpp index 55bee04b..d2c2c27f 100644 --- a/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl.cpp @@ -32,6 +32,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + + cl::Kernel kernel_add=cl::Kernel(program,"kernel_max"); kernel_add.setArg(0,*deviceObj->d_A); kernel_add.setArg(1,*deviceObj->d_B); @@ -39,17 +47,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(3,stride); kernel_add.setArg(4,lateral_stride); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl_opt.cpp index 67c044b1..b158d360 100644 --- a/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/max_pooling_bench/opencl/lib_opencl_opt.cpp @@ -33,10 +33,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel kernel_add=cl::Kernel(program,"kernel_max"); kernel_add.setArg(0,*deviceObj->d_A); @@ -45,11 +46,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, kernel_add.setArg(3,stride); kernel_add.setArg(4,lateral_stride); - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.cpp index f1459f2f..eb9b9569 100644 --- a/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.cpp @@ -46,78 +46,86 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); cl_int err; - deviceObj->d_A = new cl::Buffer(*deviceObj->context, CL_MEM_READ_ONLY, sizeof(bench_t)*size_a_matrix, nullptr, &err); - if (err != CL_SUCCESS) return false; - - // --- FIX: Switched to CL_MEM_READ_WRITE because enqueueNDRangeKernel writes --- - deviceObj->d_B = new cl::Buffer(*deviceObj->context,CL_MEM_READ_WRITE ,sizeof(bench_t)*size_b_matrix, nullptr, &err); - if (err != CL_SUCCESS) return false; - - // inicialice Arrays - return true; + + deviceObj->d_A = new cl::Buffer(*deviceObj->context, CL_MEM_READ_ONLY, sizeof(bench_t)*size_a_matrix, nullptr, &err); + if (err != CL_SUCCESS) return false; + + // --- FIX: Switched to CL_MEM_READ_WRITE because enqueueNDRangeKernel writes --- + deviceObj->d_B = new cl::Buffer(*deviceObj->context,CL_MEM_READ_WRITE ,sizeof(bench_t)*size_b_matrix, nullptr, &err); + if (err != CL_SUCCESS) return false; + + // inicialice Arrays + return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); // Enqueue writing host memory h_A to device buffer d_A cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); - if (err != CL_SUCCESS) { fprintf(stderr, "Failed to copy vector A from host to device (OpenCL error code %d)!\n", err); return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyB); + // Clock profilling start + d2hCLK.start(); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } + + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyB->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ @@ -126,7 +134,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.h b/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.h deleted file mode 100644 index 8f431120..00000000 --- a/gpu4s_benchmark/max_pooling_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./max_pooling_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/memory_bandwidth_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/memory_bandwidth_bench/cpu_functions/cpu_functions.h index 871b0f49..7f3ac7d0 100644 --- a/gpu4s_benchmark/memory_bandwidth_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/memory_bandwidth_bench/cpu_functions/cpu_functions.h @@ -38,6 +38,23 @@ union } binary_float; #endif +struct BenchmarkParameters{ + int size = 0; + unsigned int gpu = 0; + bool verification = false; + bool export_results = false; + bool export_results_gpu = false; + bool print_output = false; + bool print_timing = false; + bool csv_format = false; + bool mute_messages = false; + bool csv_format_timestamp = false; + char input_file_A[100] = ""; + char input_file_B[100] = ""; + char output_file[100] = ""; + bool profiling_clock = false; +}; + void matrix_multiplication(const bench_t* A, const bench_t* B, bench_t* C,const unsigned int n, const unsigned int m, const unsigned int w ); void matrix_convolution(const bench_t* A, bench_t* kernel, bench_t* B,const int size, const int kernel_size); //bool compare_vectors_int(const int* host,const int* device,const int size); diff --git a/gpu4s_benchmark/memory_bandwidth_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/memory_bandwidth_bench/cuda/lib_cuda.cu index bc1365de..e130d9c6 100644 --- a/gpu4s_benchmark/memory_bandwidth_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/memory_bandwidth_bench/cuda/lib_cuda.cu @@ -1,20 +1,11 @@ #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -45,112 +36,117 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ -GraficObject* deviceObj = static_cast(device_object); + GraficObject* deviceObj = static_cast(device_object); - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device copy vector B err = cudaMalloc((void **)&deviceObj->d_B, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + cudaError_t err = cudaMemcpy(deviceObj->d_B, deviceObj->d_A, sizeof(bench_t) * n, cudaMemcpyDeviceToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -160,9 +156,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format){ void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -170,7 +165,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/memory_bandwidth_bench/hip/lib_hip.cpp b/gpu4s_benchmark/memory_bandwidth_bench/hip/lib_hip.cpp index 3924f7d8..e4aa2720 100644 --- a/gpu4s_benchmark/memory_bandwidth_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/memory_bandwidth_bench/hip/lib_hip.cpp @@ -1,22 +1,13 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - /** * CUDA Kernel Device code * * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 -void init(GraficCommon* device_object, char* device_name){ + + void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -44,113 +35,123 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } + void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipError_t err = hipMemcpy(deviceObj->d_B, deviceObj->d_A, sizeof(bench_t) * n, hipMemcpyDeviceToDevice); if (err != hipSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", hipGetErrorString(err)); return; } + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -160,9 +161,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format){ void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -170,7 +170,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/memory_bandwidth_bench/main.cpp b/gpu4s_benchmark/memory_bandwidth_bench/main.cpp index 70cbd85c..e63e0a34 100644 --- a/gpu4s_benchmark/memory_bandwidth_bench/main.cpp +++ b/gpu4s_benchmark/memory_bandwidth_bench/main.cpp @@ -13,7 +13,7 @@ #define GPU_FILE "gpu_file.out" #define CPU_FILE "cpu_file.out" -int arguments_handler(int argc, char ** argv,unsigned int *size,unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,bool *print_input,char *input_file_A, char *input_file_B, bool *validation_timing, bool *mute_messages); +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters); int main(int argc, char *argv[]) { @@ -22,26 +22,25 @@ int main(int argc, char *argv[]) /////////////////////////////////////////////////////////////////////////////////////////////// // Arguments /////////////////////////////////////////////////////////////////////////////////////////////// - unsigned int size = 0, gpu = 0; - bool verification = false, export_results = false, print_output = false, print_timing = false, export_results_gpu = false, csv_format = false, print_input = false, validation_timing = false, mute_messages = false; - char input_file_A[100] = ""; - char input_file_B[100] = ""; + BenchmarkParameters *arguments_parameters = (BenchmarkParameters *)malloc(sizeof(BenchmarkParameters)); - int resolution = arguments_handler(argc,argv, &size, &gpu, &verification, &export_results, &export_results_gpu,&print_output, &print_timing, &csv_format, &print_input,input_file_A, input_file_B, &validation_timing, &mute_messages); - if (resolution == ERROR_ARGUMENTS){ + + int resolution = arguments_handler(argc,argv,arguments_parameters); + if (resolution == ERROR_ARGUMENTS) + { exit(-1); } /////////////////////////////////////////////////////////////////////////////////////////////// // VARIABLES /////////////////////////////////////////////////////////////////////////////////////////////// // linearizable versions of matrix - unsigned int size_matrix =size * size; + unsigned int size_matrix =arguments_parameters->size * arguments_parameters->size; // A input matrix - unsigned int size_A = size * size; + unsigned int size_A = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_A = sizeof(bench_t) * size_A; bench_t* A = (bench_t*) malloc(mem_size_A); // B output matrix - unsigned int size_B = size * size; + unsigned int size_B = arguments_parameters->size * arguments_parameters->size; unsigned int mem_size_B = sizeof(bench_t) * size_B; bench_t* h_B = (bench_t*) malloc(mem_size_B); bench_t* d_B = (bench_t*) malloc(mem_size_B); @@ -50,7 +49,7 @@ int main(int argc, char *argv[]) /////////////////////////////////////////////////////////////////////////////////////////////// // DATA INIT /////////////////////////////////////////////////////////////////////////////////////////////// - if (strlen(input_file_A) == 0) + if (strlen(arguments_parameters->input_file_A) == 0) { // inicialice A matrix for (int i=0; iinput_file_A, A,size_A); - // iniciate C matrix - for (int i=0; igpu, device); + if (!arguments_parameters->csv_format_timestamp && !arguments_parameters->csv_format && !arguments_parameters->mute_messages ){ printf("Using device: %s\n", device); } + // Update profiling clock mode + mem_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + mem_bench->profiling_clock = true; + #endif + + // init memory device_memory_init(mem_bench, size_A , size_B ); // copy memory to device @@ -130,11 +114,11 @@ int main(int argc, char *argv[]) copy_memory_to_host(mem_bench, h_B, size_B); // get time - if (print_timing || csv_format) + if (arguments_parameters->print_timing || arguments_parameters->csv_format || arguments_parameters->csv_format_timestamp) { - get_elapsed_time(mem_bench, csv_format); + get_elapsed_time(mem_bench, arguments_parameters->csv_format); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT for (int i=0; iverification) { - if (print_timing) + Clock cpuKernelCLK; + cpuKernelCLK.start(); + memcpy(d_B, A, mem_size_A); + cpuKernelCLK.end(); + if (arguments_parameters->print_timing) { - printf("CPU Time %.0f milliseconds\n", 0.0f); + printf("CPU Time %.0f milliseconds\n", cpuKernelCLK.getElapsedMS()); } - if (print_output) + if (arguments_parameters->print_output) { #ifdef INT for (int i=0; iexport_results){ print_double_hexadecimal_values(GPU_FILE, h_B, size_B); print_double_hexadecimal_values(CPU_FILE, A, size_B); } } - if (export_results_gpu) + if (arguments_parameters->export_results_gpu) { print_double_hexadecimal_values(GPU_FILE, h_B, size_B); } @@ -193,6 +179,7 @@ int main(int argc, char *argv[]) /////////////////////////////////////////////////////////////////////////////////////////////// // clean device memory clean(mem_bench); + free(arguments_parameters); // free object memory free(mem_bench); free(A); @@ -203,27 +190,45 @@ return 0; // Arguments part - void print_usage(const char * appName) { - printf("Usage: %s -s Size -k [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); - printf(" -s Size : set size of x matrix to be copy\n"); + printf("Usage: %s -s Size [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); + printf(" -s Size : set size of x and y of matrices A and B with Size \n"); printf(" -e: exports the results of the output and the verification in hexadecimal format (this enables the verification of the results) \n"); printf(" -v: verify the output of the gpu program with the cpu output \n"); printf(" -g: exports the results of the output \n"); printf(" -o: prints the results\n"); printf(" -t: prints the timing\n"); printf(" -c: prints the timing in csv format\n"); - printf(" -q: prints input values\n"); + printf(" -C: prints the timing in csv format with timestamp\n"); printf(" -i: pass input data and the result and compares\n"); printf(" -d: selects GPU\n"); - printf(" -x: prints the timing of the validation. Only the sequential time of the application will be displayed\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); +} + +void init_arguments(BenchmarkParameters* arguments_parameters){ + arguments_parameters->size = 0; + arguments_parameters->gpu = 0; + arguments_parameters->verification = false; + arguments_parameters->export_results = false; + arguments_parameters->export_results_gpu = false; + arguments_parameters->print_output = false; + arguments_parameters->print_timing = false; + arguments_parameters->csv_format = false; + arguments_parameters->mute_messages = false; + arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; + // --- Properly clear character arrays --- + arguments_parameters->input_file_A[0] = '\0'; + arguments_parameters->input_file_B[0] = '\0'; + arguments_parameters->output_file[0] = '\0'; } -int arguments_handler(int argc, char ** argv,unsigned int *size,unsigned int *gpu,bool *verification, bool *export_results, bool *export_results_gpu, bool *print_output, bool *print_timing, bool *csv_format,bool *print_input,char *input_file_A, char *input_file_B, bool *validation_timing, bool *mute_messages) { +int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ + init_arguments(arguments_parameters); if (argc == 1){ printf("-s need to be set\n\n"); print_usage(argv[0]); @@ -233,37 +238,36 @@ int arguments_handler(int argc, char ** argv,unsigned int *size,unsigned int *gp { switch (argv[args][1]) { // comon part - case 'v' : *verification = true;break; - case 'e' : *verification = true; *export_results= true;break; - case 'o' : *print_output = true;break; - case 't' : *print_timing = true;break; - case 'c' : *csv_format = true;break; - case 'g' : *export_results_gpu = true;break; - case 'q' : *print_input = true;break; - case 'd' : args +=1; *gpu = atoi(argv[args]);break; - // specific - case 'i' : args +=1; - strcpy(input_file_A,argv[args]); + case 'v' : arguments_parameters->verification = true;break; + case 'e' : arguments_parameters->verification = true; arguments_parameters->export_results= true;break; + case 'o' : arguments_parameters->print_output = true;break; + case 't' : arguments_parameters->print_timing = true;break; + case 'c' : arguments_parameters->csv_format = true;break; + case 'C' : arguments_parameters->csv_format_timestamp = true;break; + case 'g' : arguments_parameters->export_results_gpu = true;break; + case 'd' : args +=1; arguments_parameters->gpu = atoi(argv[args]);break; + case 'f' : arguments_parameters->mute_messages = true;break; args +=1; - strcpy(input_file_B,argv[args]); + strcpy(arguments_parameters->output_file,argv[args]); break; - case 'x' : *validation_timing = true;break; - case 'f' : *mute_messages = true;break; - args +=1; - strcpy(input_file_B,argv[args]); - break; - case 's' : args +=1; *size = atoi(argv[args]);break; + case 'i' : args +=1; + strcpy(arguments_parameters->input_file_A,argv[args]); + args +=1; + strcpy(arguments_parameters->input_file_B,argv[args]); //TODO FIX with final version of input files + break; + case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } } - if ( *size <= 0){ + if ( arguments_parameters->size <= 0){ printf("-s need to be set\n\n"); print_usage(argv[0]); return ERROR_ARGUMENTS; } - if (*mute_messages){ - *csv_format = false; + if (arguments_parameters->mute_messages){ + arguments_parameters->csv_format = false; } return OK_ARGUMENTS; } diff --git a/gpu4s_benchmark/memory_bandwidth_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/memory_bandwidth_bench/opencl/lib_opencl.cpp index d50c227d..133d2362 100644 --- a/gpu4s_benchmark/memory_bandwidth_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/memory_bandwidth_bench/opencl/lib_opencl.cpp @@ -4,16 +4,6 @@ #include #include "kernel.cl" -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - - -//#define BLOCK_SIZE 256 void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -53,6 +43,7 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ GraficObject* deviceObj = static_cast(device_object); cl_int err; + deviceObj->d_A = new cl::Buffer(*deviceObj->context,CL_MEM_READ_WRITE ,sizeof(bench_t)*size_a_matrix, nullptr, &err); if (err != CL_SUCCESS) return false; @@ -65,84 +56,96 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); // Enqueue writing host memory h_A to device buffer d_A cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); - if (err != CL_SUCCESS) { fprintf(stderr, "Failed to copy vector A from host to device (OpenCL error code %d)!\n", err); return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); deviceObj->queue->enqueueCopyBuffer(*deviceObj->d_A,*deviceObj->d_B, 0,0,sizeof(bench_t)*n,NULL, deviceObj->evt_copyB); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - //elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + //elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/relu_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/relu_bench/cpu_functions/cpu_functions.h index 8b879a64..8d69877e 100644 --- a/gpu4s_benchmark/relu_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/relu_bench/cpu_functions/cpu_functions.h @@ -56,6 +56,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/relu_bench/cuda/cuda_common.cu b/gpu4s_benchmark/relu_bench/cuda/cuda_common.cu index 9e73d56d..181de5e3 100644 --- a/gpu4s_benchmark/relu_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/relu_bench/cuda/cuda_common.cu @@ -35,36 +35,28 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -72,49 +64,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); - cudaEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; } + + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -123,7 +124,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); } else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -133,9 +134,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -143,7 +143,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/relu_bench/cuda/cuda_common.h b/gpu4s_benchmark/relu_bench/cuda/cuda_common.h deleted file mode 100644 index c93f2f07..00000000 --- a/gpu4s_benchmark/relu_bench/cuda/cuda_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./relu_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/relu_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/relu_bench/cuda/lib_cuda.cu index 5d1c894c..724b0b78 100644 --- a/gpu4s_benchmark/relu_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/relu_bench/cuda/lib_cuda.cu @@ -29,18 +29,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + relu_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/relu_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/relu_bench/cuda/lib_cuda_lib.cu index 5463b9c0..a9763e88 100644 --- a/gpu4s_benchmark/relu_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/relu_bench/cuda/lib_cuda_lib.cu @@ -20,18 +20,19 @@ #endif void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ - GraficObject* deviceObj = static_cast(device_object); - // CUDNN settings + GraficObject* deviceObj = static_cast(device_object); + // CUDNN settings const bench_t alf = 1; const bench_t bet = 0; cudnnHandle_t cudnn; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); checkCUDNN(cudnnCreate(&cudnn)); + // create input tensor cudnnTensorDescriptor_t input_descriptor; checkCUDNN(cudnnCreateTensorDescriptor(&input_descriptor)); @@ -69,13 +70,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, &bet, output_descriptor, deviceObj->d_B)); - + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cuDNN cudnnDestroyTensorDescriptor(input_descriptor); diff --git a/gpu4s_benchmark/relu_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/relu_bench/cuda/lib_cuda_opt.cu index 50438c5a..e9e3417a 100644 --- a/gpu4s_benchmark/relu_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/relu_bench/cuda/lib_cuda_opt.cu @@ -30,17 +30,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE); dim3 dimGrid(ceil(float((n*n))/(dimBlock.x))); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + relu_kernel<<>>(deviceObj->d_A, deviceObj->d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/relu_bench/hip/hip_common.cpp b/gpu4s_benchmark/relu_bench/hip/hip_common.cpp index ca4911ae..f219ccaa 100644 --- a/gpu4s_benchmark/relu_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/relu_bench/hip/hip_common.cpp @@ -6,8 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -37,36 +35,34 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_terr = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } return true; } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -74,49 +70,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -124,7 +129,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -134,9 +139,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -144,7 +148,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/relu_bench/hip/hip_common.h b/gpu4s_benchmark/relu_bench/hip/hip_common.h deleted file mode 100644 index f03db3a5..00000000 --- a/gpu4s_benchmark/relu_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./relu_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/relu_bench/hip/lib_hip.cpp b/gpu4s_benchmark/relu_bench/hip/lib_hip.cpp index 1057370e..01e28da6 100644 --- a/gpu4s_benchmark/relu_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/relu_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void relu_kernel(const bench_t *A, bench_t *B, const int size) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -31,18 +30,21 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((relu_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/relu_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/relu_bench/hip/lib_hip_opt.cpp index 2bfb5e3e..bfa402f0 100644 --- a/gpu4s_benchmark/relu_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/relu_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 -__global__ void + + __global__ void relu_kernel(const bench_t *A, bench_t *B, const int size) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -32,17 +31,20 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE); dim3 dimGrid(ceil(float((n*n))/(dimBlock.x))); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((relu_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/relu_bench/main.cpp b/gpu4s_benchmark/relu_bench/main.cpp index 8c558012..90329797 100644 --- a/gpu4s_benchmark/relu_bench/main.cpp +++ b/gpu4s_benchmark/relu_bench/main.cpp @@ -119,6 +119,14 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + relu_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + relu_bench->profiling_clock = true; + #endif + // init memory device_memory_init(relu_bench, arguments_parameters->size * arguments_parameters->size, arguments_parameters->size * arguments_parameters->size); @@ -241,6 +249,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } // Arguments part @@ -261,6 +270,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } @@ -292,6 +302,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -305,4 +316,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/relu_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/relu_bench/opencl/lib_opencl.cpp index d75b2a17..ccee1897 100644 --- a/gpu4s_benchmark/relu_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/relu_bench/opencl/lib_opencl.cpp @@ -3,8 +3,6 @@ #include "../benchmark_library.h" #include #include "GEN_kernel.hcl" -#include "../cpu_functions/cpu_functions.h" - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int w){ GraficObject* deviceObj = static_cast(device_object); @@ -34,20 +32,25 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + cl::Kernel kernel_add=cl::Kernel(program,"kernel_relu"); kernel_add.setArg(0,*deviceObj->d_A); kernel_add.setArg(1,*deviceObj->d_B); kernel_add.setArg(2,n); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif - + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/relu_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/relu_bench/opencl/lib_opencl_opt.cpp index 51ed269b..b1e5ea4c 100644 --- a/gpu4s_benchmark/relu_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/relu_bench/opencl/lib_opencl_opt.cpp @@ -3,7 +3,6 @@ #include "../benchmark_library.h" #include #include "GEN_kernel_opt.hcl" -#include "../cpu_functions/cpu_functions.h" void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, unsigned int w){ GraficObject* deviceObj = static_cast(device_object); @@ -33,20 +32,26 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + + cl::Kernel kernel_add=cl::Kernel(program,"kernel_relu"); kernel_add.setArg(0,*deviceObj->d_A); kernel_add.setArg(1,*deviceObj->d_B); kernel_add.setArg(2,n); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(kernel_add,cl::NullRange,global,local, NULL, deviceObj->evt); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/relu_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/relu_bench/opencl/opencl_common.cpp index 0e291be8..9b915f26 100644 --- a/gpu4s_benchmark/relu_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/relu_bench/opencl/opencl_common.cpp @@ -7,8 +7,6 @@ * ======================================================================= */ #include "../benchmark_library.h" - - void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -61,25 +59,25 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); // Enqueue writing host memory h_A to device buffer d_A cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); - if (err != CL_SUCCESS) { fprintf(stderr, "Failed to copy vector A from host to device (OpenCL error code %d)!\n", err); return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } #ifdef UNIFIED_MEMORY @@ -133,16 +131,24 @@ void device_unified_memory_init_copy(GraficCommon* device_object, bench_t* &A, b void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyB); + // Clock profilling start + d2hCLK.start(); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } + + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } #ifdef UNIFIED_MEMORY @@ -162,25 +168,25 @@ void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyB->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } #ifdef UNIFIED_MEMORY elapsed_h_d = h2dTotal; @@ -193,7 +199,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/relu_bench/opencl/opencl_common.h b/gpu4s_benchmark/relu_bench/opencl/opencl_common.h deleted file mode 100644 index d87ca7d3..00000000 --- a/gpu4s_benchmark/relu_bench/opencl/opencl_common.h +++ /dev/null @@ -1,20 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./relu_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; - - #ifdef UNIFIED_MEMORY - float h2dTotal = 0; - #endif -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/cpu_functions/cpu_functions.h b/gpu4s_benchmark/softmax_bench/cpu_functions/cpu_functions.h index 29b5a2c0..85e87a1a 100644 --- a/gpu4s_benchmark/softmax_bench/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/softmax_bench/cpu_functions/cpu_functions.h @@ -57,6 +57,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/softmax_bench/cuda/cuda_common.cu b/gpu4s_benchmark/softmax_bench/cuda/cuda_common.cu index 65dc3dbb..19c8d017 100644 --- a/gpu4s_benchmark/softmax_bench/cuda/cuda_common.cu +++ b/gpu4s_benchmark/softmax_bench/cuda/cuda_common.cu @@ -35,45 +35,32 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } // Allocate the device input sum_d_B err = cudaMalloc((void **)&deviceObj->sum_d_B, sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + if (err != cudaSuccess) return false; return true; - } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { @@ -81,49 +68,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); + + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); - cudaEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + cudaError_t err = cudaMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; } + + // profilling end + cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host);// wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -131,7 +127,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -141,9 +137,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -151,7 +146,6 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); diff --git a/gpu4s_benchmark/softmax_bench/cuda/cuda_common.h b/gpu4s_benchmark/softmax_bench/cuda/cuda_common.h deleted file mode 100644 index 5006faf4..00000000 --- a/gpu4s_benchmark/softmax_bench/cuda/cuda_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./softmax_bench) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda.cu b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda.cu index 7dac546e..386fdcf2 100644 --- a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda.cu @@ -7,6 +7,7 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ + __global__ void softmax_kernel(const bench_t *A, bench_t *B, bench_t *sum_d_B,const int size) { @@ -24,6 +25,7 @@ softmax_kernel(const bench_t *A, bench_t *B, bench_t *sum_d_B,const int size) atomicAdd(sum_d_B, B[i*size+j]); } } + __global__ void softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) { @@ -34,23 +36,25 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) } } - void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + softmax_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->sum_d_B, n); softmax_finish_kernel<<>>(deviceObj->d_B, deviceObj->sum_d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_lib.cu b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_lib.cu index 5d5147ca..6a033c8b 100644 --- a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_lib.cu +++ b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_lib.cu @@ -20,18 +20,19 @@ #endif void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ - GraficObject* deviceObj = static_cast(device_object); - // CUDNN settings + GraficObject* deviceObj = static_cast(device_object); + // CUDNN settings const bench_t alf = 1; const bench_t bet = 0; cudnnHandle_t cudnn; + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); checkCUDNN(cudnnCreate(&cudnn)); + // create input tensor cudnnTensorDescriptor_t input_descriptor; checkCUDNN(cudnnCreateTensorDescriptor(&input_descriptor)); @@ -64,16 +65,16 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, output_descriptor, deviceObj->d_B)); + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); // destroy cuDNN cudnnDestroyTensorDescriptor(input_descriptor); cudnnDestroyTensorDescriptor(output_descriptor); - cudnnDestroy(cudnn); } \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_opt.cu index bb548128..024a70fe 100644 --- a/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/softmax_bench/cuda/lib_cuda_opt.cu @@ -52,23 +52,23 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock, dimGrid; - dimBlock = dim3(BLOCK_SIZE); dimGrid = dim3(ceil(float((n*n))/(dimBlock.x))); - - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + softmax_kernel<<>>(deviceObj->d_A, deviceObj->d_B, deviceObj->sum_d_B, n); softmax_finish_kernel<<>>(deviceObj->d_B, deviceObj->sum_d_B, n); + + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/hip/hip_common.cpp b/gpu4s_benchmark/softmax_bench/hip/hip_common.cpp index f5fbe6d6..cc466c49 100644 --- a/gpu4s_benchmark/softmax_bench/hip/hip_common.cpp +++ b/gpu4s_benchmark/softmax_bench/hip/hip_common.cpp @@ -35,45 +35,39 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipSuccess; + err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; - if (err != hipSuccess) - { - return false; - } // Allocate the device input sum_d_B err = hipMalloc((void **)&deviceObj->sum_d_B, sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; return true; - } void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -81,49 +75,58 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i return; } - (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_C, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); +} float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -131,7 +134,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -141,9 +144,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -151,7 +153,6 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); diff --git a/gpu4s_benchmark/softmax_bench/hip/hip_common.h b/gpu4s_benchmark/softmax_bench/hip/hip_common.h deleted file mode 100644 index a714d206..00000000 --- a/gpu4s_benchmark/softmax_bench/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./softmax_bench) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/softmax_bench/hip/lib_hip.cpp b/gpu4s_benchmark/softmax_bench/hip/lib_hip.cpp index 2cc9f175..2eec2e61 100644 --- a/gpu4s_benchmark/softmax_bench/hip/lib_hip.cpp +++ b/gpu4s_benchmark/softmax_bench/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 32 -__global__ void + + __global__ void softmax_kernel(const bench_t *A, bench_t *B, bench_t *sum_d_B,const int size) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; @@ -40,19 +39,22 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x), ceil(float(m)/dimBlock.y)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((softmax_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->sum_d_B, n); hipLaunchKernelGGL((softmax_finish_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_B, deviceObj->sum_d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/softmax_bench/hip/lib_hip_opt.cpp b/gpu4s_benchmark/softmax_bench/hip/lib_hip_opt.cpp index ce7aabd3..ef2bd5d9 100644 --- a/gpu4s_benchmark/softmax_bench/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/softmax_bench/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" #include "math.h" @@ -8,8 +7,8 @@ * Computes the vector addition of A and B into C. The 3 vectors have the same * number of elements numElements. */ -//#define BLOCK_SIZE 1024 -__global__ void + + __global__ void softmax_kernel(const bench_t *A, bench_t *B, bench_t *sum_d_B,const int size) { unsigned int i = blockIdx.x * blockDim.x + threadIdx.x; unsigned int tid = threadIdx.x; @@ -54,23 +53,23 @@ softmax_finish_kernel(bench_t *B, bench_t *sum_d_B,const int size) void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m,unsigned int w){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock, dimGrid; - dimBlock = dim3(BLOCK_SIZE); dimGrid = dim3(ceil(float((n*n))/(dimBlock.x))); - - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + hipLaunchKernelGGL((softmax_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, deviceObj->sum_d_B, n); hipLaunchKernelGGL((softmax_finish_kernel), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_B, deviceObj->sum_d_B, n); + + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/main.cpp b/gpu4s_benchmark/softmax_bench/main.cpp index 8e2caba3..fc3ef424 100644 --- a/gpu4s_benchmark/softmax_bench/main.cpp +++ b/gpu4s_benchmark/softmax_bench/main.cpp @@ -111,6 +111,15 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + softmax_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + softmax_bench->profiling_clock = true; + #endif + + // init memory device_memory_init(softmax_bench, arguments_parameters->size * arguments_parameters->size, arguments_parameters->size * arguments_parameters->size); // copy memory to device @@ -227,6 +236,7 @@ void print_usage(const char * appName) printf(" -i: pass input data and the result and compares\n"); printf(" -d: selects GPU\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -241,6 +251,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } @@ -272,6 +283,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -285,4 +297,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl.cpp b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl.cpp index 05caef12..cf746217 100644 --- a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl.cpp @@ -31,6 +31,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + + cl::Kernel softmax_kernel=cl::Kernel(program,"kernel_softmax"); softmax_kernel.setArg(0,*deviceObj->d_A); softmax_kernel.setArg(1,*deviceObj->d_B); @@ -42,19 +50,17 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, softmax_end_kernel.setArg(0,*deviceObj->d_B); softmax_end_kernel.setArg(1,*deviceObj->sum_d_B); softmax_end_kernel.setArg(2,n); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif - // Enqueue first kernel + // Enqueue both kernels deviceObj->queue->enqueueNDRangeKernel(softmax_kernel,cl::NullRange,global,local, NULL, deviceObj->evt); - deviceObj->queue->enqueueNDRangeKernel(softmax_end_kernel,cl::NullRange,global,local, NULL, deviceObj->evt_complemet); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_lib.cpp b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_lib.cpp index ec314c1e..5efd1ad5 100644 --- a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_lib.cpp +++ b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_lib.cpp @@ -35,10 +35,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); cl::Kernel softmax_kernel=cl::Kernel(program,"kernel_softmax"); softmax_kernel.setArg(0,*deviceObj->d_A); @@ -48,17 +49,18 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, deviceObj->queue->enqueueNDRangeKernel(softmax_kernel,cl::NullRange,global,local, NULL, deviceObj->evt); - cl::Kernel softmax_end_kernel=cl::Kernel(program,"kernel_softmax_end"); softmax_end_kernel.setArg(0,*deviceObj->d_B); softmax_end_kernel.setArg(1,*deviceObj->sum_d_B); softmax_end_kernel.setArg(2,n); deviceObj->queue->enqueueNDRangeKernel(softmax_end_kernel,cl::NullRange,global,local, NULL, deviceObj->evt_complemet); + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } diff --git a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_opt.cpp b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_opt.cpp index d2ad3cfe..2eaf95bb 100644 --- a/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_opt.cpp +++ b/gpu4s_benchmark/softmax_bench/opencl/lib_opencl_opt.cpp @@ -33,6 +33,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, std::cout<<" Error building: "<(deviceObj->default_device)<<"\n"; exit(1); } + + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); + cl::Kernel softmax_kernel=cl::Kernel(program,"kernel_softmax"); softmax_kernel.setArg(0,*deviceObj->d_A); softmax_kernel.setArg(1,*deviceObj->d_B); @@ -44,16 +51,14 @@ void execute_kernel(GraficCommon* device_object, unsigned int n, unsigned int m, softmax_end_kernel.setArg(1,*deviceObj->sum_d_B); softmax_end_kernel.setArg(2,n); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - kernelCLK.start(); - #endif - deviceObj->queue->enqueueNDRangeKernel(softmax_kernel,cl::NullRange,global,local, NULL, deviceObj->evt); deviceObj->queue->enqueueNDRangeKernel(softmax_end_kernel,cl::NullRange,global,local, NULL, deviceObj->evt_complemet); + + // Wait for completion before stopping the clock deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/softmax_bench/opencl/opencl_common.cpp b/gpu4s_benchmark/softmax_bench/opencl/opencl_common.cpp index 26ac712c..4094c365 100644 --- a/gpu4s_benchmark/softmax_bench/opencl/opencl_common.cpp +++ b/gpu4s_benchmark/softmax_bench/opencl/opencl_common.cpp @@ -63,67 +63,79 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); // Enqueue writing host memory h_A to device buffer d_A cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); - if (err != CL_SUCCESS) { fprintf(stderr, "Failed to copy vector A from host to device (OpenCL error code %d)!\n", err); return; } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); + + + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyB); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + + return; - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyB); - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + } + + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyB->wait(); - float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - elapsed += deviceObj->evt_complemet->getProfilingInfo() - deviceObj->evt_complemet->getProfilingInfo(); - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + elapsed += deviceObj->evt_complemet->getProfilingInfo() - deviceObj->evt_complemet->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0,current_time); @@ -131,7 +143,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0); diff --git a/gpu4s_benchmark/softmax_bench/opencl/opencl_common.h b/gpu4s_benchmark/softmax_bench/opencl/opencl_common.h deleted file mode 100644 index 4cc751df..00000000 --- a/gpu4s_benchmark/softmax_bench/opencl/opencl_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file opencl_common.h (./softmax_bench) - * @brief Shared declarations, data structures, and timing utilities - * for OpenCL benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif diff --git a/gpu4s_benchmark/wavelet_transform/cpu_functions/cpu_functions.h b/gpu4s_benchmark/wavelet_transform/cpu_functions/cpu_functions.h index 4fdf9634..694d6fc3 100644 --- a/gpu4s_benchmark/wavelet_transform/cpu_functions/cpu_functions.h +++ b/gpu4s_benchmark/wavelet_transform/cpu_functions/cpu_functions.h @@ -61,6 +61,7 @@ struct BenchmarkParameters{ bool csv_format = false; bool mute_messages = false; bool csv_format_timestamp = false; + bool profiling_clock = false; char input_file_A[100] = ""; char input_file_B[100] = ""; }; diff --git a/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.cu b/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.cu index 6a0879eb..263222e1 100644 --- a/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.cu +++ b/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.cu @@ -35,43 +35,26 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - cudaError_t err = cudaSuccess; - err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device input vector A + cudaError_t err = cudaMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; // Allocate the device input vector B err = cudaMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } #ifdef INT - // if int don't add the allocation + // if int don't add the allocation #else - // Allocate the device low_filter - err = cudaMalloc((void **)&deviceObj->low_filter, LOWPASSFILTERSIZE * sizeof(bench_t)); + // Allocate the device low_filter + err = cudaMalloc((void **)&deviceObj->low_filter, LOWPASSFILTERSIZE * sizeof(bench_t)); + if (err != cudaSuccess) return false; - if (err != cudaSuccess) - { - return false; - } - - // Allocate the device high_filter - err = cudaMalloc((void **)&deviceObj->high_filter, HIGHPASSFILTERSIZE * sizeof(bench_t)); - - if (err != cudaSuccess) - { - return false; - } + // Allocate the device high_filter + err = cudaMalloc((void **)&deviceObj->high_filter, HIGHPASSFILTERSIZE * sizeof(bench_t)); + if (err != cudaSuccess) return false; #endif return true; @@ -79,82 +62,90 @@ GraficObject* deviceObj = static_cast(device_object); void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_device); + cudaError_t err = cudaMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, cudaMemcpyHostToDevice); if (err != cudaSuccess) { fprintf(stderr, "Failed to copy vector A from host to device (error code %s)!\n", cudaGetErrorString(err)); return; } - #ifdef INT - // if int don't add the copy of the filters + // if int don't add the copy of the filters #else - err = cudaMemcpy(deviceObj->low_filter, lowpass_filter, sizeof(bench_t) * LOWPASSFILTERSIZE, cudaMemcpyHostToDevice); - if (err != cudaSuccess) - { - fprintf(stderr, "Failed to copy vector lowpass filter from host to device (error code %s)!\n", cudaGetErrorString(err)); - return; - } - - err = cudaMemcpy(deviceObj->high_filter, highpass_filter, sizeof(bench_t) * HIGHPASSFILTERSIZE, cudaMemcpyHostToDevice); - if (err != cudaSuccess) - { - fprintf(stderr, "Failed to copy vector highpass filter from host to device (error code %s)!\n", cudaGetErrorString(err)); - return; - } + err = cudaMemcpy(deviceObj->low_filter, lowpass_filter, sizeof(bench_t) * LOWPASSFILTERSIZE, cudaMemcpyHostToDevice); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector lowpass filter from host to device (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + err = cudaMemcpy(deviceObj->high_filter, highpass_filter, sizeof(bench_t) * HIGHPASSFILTERSIZE, cudaMemcpyHostToDevice); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector highpass filter from host to device (error code %s)!\n", cudaGetErrorString(err)); + return; + } #endif + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_device); - - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif + h2dCLK.end(); + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); cudaEventRecord(*deviceObj->start_memory_copy_host); - cudaMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + + cudaError_t err = cudaMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), cudaMemcpyDeviceToHost); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", cudaGetErrorString(err)); + return; + } + + // profilling end cudaEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - cudaEventSynchronize(*deviceObj->stop_memory_copy_host); + cudaEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + cudaEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + cudaEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + cudaEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -162,7 +153,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -172,9 +163,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - cudaError_t err = cudaSuccess; - err = cudaFree(deviceObj->d_A); + cudaError_t err = cudaFree(deviceObj->d_A); if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", cudaGetErrorString(err)); @@ -182,14 +172,25 @@ void clean(GraficCommon* device_object){ } err = cudaFree(deviceObj->d_B); - if (err != cudaSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", cudaGetErrorString(err)); return; } + err = cudaFree(deviceObj->low_filter); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to free device vector low_filter (error code %s)!\n", cudaGetErrorString(err)); + return; + } + err = cudaFree(deviceObj->high_filter); + if (err != cudaSuccess) + { + fprintf(stderr, "Failed to free device vector high_filter (error code %s)!\n", cudaGetErrorString(err)); + return; + } // delete events delete deviceObj->start; diff --git a/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.h b/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.h deleted file mode 100644 index 354935c9..00000000 --- a/gpu4s_benchmark/wavelet_transform/cuda/cuda_common.h +++ /dev/null @@ -1,17 +0,0 @@ -/** * ==================================================================== - * @file cuda_common.h (./wavelet_transform) - * @brief Shared declarations, data structures, and timing utilities - * for CUDA benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda.cu b/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda.cu index 8a563c32..abd393df 100644 --- a/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda.cu +++ b/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda.cu @@ -115,12 +115,13 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); + #ifdef INT wavelet_transform<<>>(deviceObj->d_A, deviceObj->d_B, n); wavelet_transform_low<<>>(deviceObj->d_A, deviceObj->d_B, n); @@ -128,10 +129,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ wavelet_transform<<>>(deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda_opt.cu b/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda_opt.cu index 8587ba55..bfd124e7 100644 --- a/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda_opt.cu +++ b/gpu4s_benchmark/wavelet_transform/cuda/lib_cuda_opt.cu @@ -156,17 +156,15 @@ wavelet_transform(const bench_t *A, bench_t *B, const int n, const bench_t *lowp #endif void execute_kernel(GraficCommon* device_object, unsigned int n){ - -GraficObject* deviceObj = static_cast(device_object); - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); cudaEventRecord(*deviceObj->start); - #ifdef INT + #ifdef INT dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); dim3 dimGrid(ceil(float(n/NUMBERSUBDIVISIONS)/dimBlock.x)); @@ -181,24 +179,23 @@ GraficObject* deviceObj = static_cast(device_object); wavelet_transform<<>>(deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); wavelet_transform_low<<>>(deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); } - - #else - cudaStream_t cuda_streams[2]; - dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); - dim3 dimGrid(ceil(float(n)/dimBlock.x)); - for (unsigned int streams = 0; streams < 2; ++streams) - { - cudaStreamCreate(&cuda_streams[streams]); - } - wavelet_transform<<>>(deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); - wavelet_transform_high<<>>(deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); + cudaStream_t cuda_streams[2]; + dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); + dim3 dimGrid(ceil(float(n)/dimBlock.x)); + for (unsigned int streams = 0; streams < 2; ++streams) + { + cudaStreamCreate(&cuda_streams[streams]); + } + wavelet_transform<<>>(deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); + wavelet_transform_high<<>>(deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); #endif + // profilling end cudaEventRecord(*deviceObj->stop); + cudaDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - cudaDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/wavelet_transform/hip/hip_common.cpp b/gpu4s_benchmark/wavelet_transform/hip/hip_common.cpp index d465b254..1fe98b77 100644 --- a/gpu4s_benchmark/wavelet_transform/hip/hip_common.cpp +++ b/gpu4s_benchmark/wavelet_transform/hip/hip_common.cpp @@ -6,7 +6,6 @@ * ESA-PL Strong Copyleft – v2.5 * ======================================================================= */ #include "../benchmark_library.h" -#include "hip/hip_runtime.h" void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); @@ -36,43 +35,33 @@ void init(GraficCommon* device_object, int platform ,int device, char* device_na } bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, unsigned int size_b_matrix){ + GraficObject* deviceObj = static_cast(device_object); -GraficObject* deviceObj = static_cast(device_object); - - // Allocate the device input vector A - hipError_t err = hipSuccess; - err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); - - if (err != hipSuccess) + // FIX: create a dumb obj to sync the profling clock + if (deviceObj->profiling_clock) { - return false; + hipDumbSync(); } + + // Allocate the device input vector A + hipError_t err = hipSuccess; + err = hipMalloc((void **)&deviceObj->d_A, size_a_matrix * sizeof(bench_t)); + if (err != hipSuccess) return false; // Allocate the device input vector B err = hipMalloc((void **)&deviceObj->d_B, size_b_matrix * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + if (err != hipSuccess) return false; + #ifdef INT - // if int don't add the allocation + // if int don't add the allocation #else - // Allocate the device low_filter - err = hipMalloc((void **)&deviceObj->low_filter, LOWPASSFILTERSIZE * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + // Allocate the device low_filter + err = hipMalloc((void **)&deviceObj->low_filter, LOWPASSFILTERSIZE * sizeof(bench_t)); + if (err != hipSuccess) return false; - // Allocate the device high_filter - err = hipMalloc((void **)&deviceObj->high_filter, HIGHPASSFILTERSIZE * sizeof(bench_t)); - - if (err != hipSuccess) - { - return false; - } + // Allocate the device high_filter + err = hipMalloc((void **)&deviceObj->high_filter, HIGHPASSFILTERSIZE * sizeof(bench_t)); + if (err != hipSuccess) return false; #endif return true; @@ -80,12 +69,13 @@ GraficObject* deviceObj = static_cast(device_object); void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif - + // profilling start + h2dCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_device); + hipError_t err = hipMemcpy(deviceObj->d_A, h_A, sizeof(bench_t) * size_a, hipMemcpyHostToDevice); if (err != hipSuccess) { @@ -94,67 +84,75 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i } #ifdef INT - // if int don't add the copy of the filters + // if int don't add the copy of the filters #else - err = hipMemcpy(deviceObj->low_filter, lowpass_filter, sizeof(bench_t) * LOWPASSFILTERSIZE, hipMemcpyHostToDevice); - if (err != hipSuccess) - { - fprintf(stderr, "Failed to copy vector lowpass filter from host to device (error code %s)!\n", hipGetErrorString(err)); - return; - } - - err = hipMemcpy(deviceObj->high_filter, highpass_filter, sizeof(bench_t) * HIGHPASSFILTERSIZE, hipMemcpyHostToDevice); - if (err != hipSuccess) - { - fprintf(stderr, "Failed to copy vector highpass filter from host to device (error code %s)!\n", hipGetErrorString(err)); - return; - } + err = hipMemcpy(deviceObj->low_filter, lowpass_filter, sizeof(bench_t) * LOWPASSFILTERSIZE, hipMemcpyHostToDevice); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector lowpass filter from host to device (error code %s)!\n", hipGetErrorString(err)); + return; + } + + err = hipMemcpy(deviceObj->high_filter, highpass_filter, sizeof(bench_t) * HIGHPASSFILTERSIZE, hipMemcpyHostToDevice); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector highpass filter from host to device (error code %s)!\n", hipGetErrorString(err)); + return; + } #endif + // profilling end (void)hipEventRecord(*deviceObj->stop_memory_copy_device); + h2dCLK.end(); - #ifdef PROFILING_CLOCK - h2dCLK.end(); - #endif - + // store the h2d time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedMS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_B, int size){ GraficObject* deviceObj = static_cast(device_object); + // device -> host + Clock d2hCLK; - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif - + // profilling start + d2hCLK.start(); (void)hipEventRecord(*deviceObj->start_memory_copy_host); - hipMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); - (void)hipEventRecord(*deviceObj->stop_memory_copy_host); - #ifdef PROFILING_CLOCK - d2hCLK.end(); - #endif + hipError_t err = hipMemcpy(h_B, deviceObj->d_B, size * sizeof(bench_t), hipMemcpyDeviceToHost); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to copy vector B from device to host (error code %s)!\n", hipGetErrorString(err)); + return; + } + + // profilling end + (void)hipEventRecord(*deviceObj->stop_memory_copy_host); + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedMS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); - (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); + (void)hipEventSynchronize(*deviceObj->stop_memory_copy_host); // wait + float milliseconds_h_d = 0, milliseconds = 0, milliseconds_d_h = 0; - // memory transfer time host-device - (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); - // kernel time - (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); - // memory transfer time device-host - (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); - - #ifdef PROFILING_CLOCK + + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - milliseconds_h_d = h2dCLK.getElapsedMS(); - milliseconds = kernelCLK.getElapsedMS(); - milliseconds_d_h = d2hCLK.getElapsedMS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + milliseconds_h_d = deviceObj->h2d_elapsed_time; + milliseconds = deviceObj->elapsed_time; + milliseconds_d_h = deviceObj->d2h_elapsed_time; + }else{ + // memory transfer time host-device + (void)hipEventElapsedTime(&milliseconds_h_d, *deviceObj->start_memory_copy_device, *deviceObj->stop_memory_copy_device); + // kernel time + (void)hipEventElapsedTime(&milliseconds, *deviceObj->start, *deviceObj->stop); + // memory transfer time device-host + (void)hipEventElapsedTime(&milliseconds_d_h, *deviceObj->start_memory_copy_host, *deviceObj->stop_memory_copy_host); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", milliseconds_h_d,milliseconds,milliseconds_d_h,current_time); @@ -162,7 +160,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", milliseconds_h_d,milliseconds,milliseconds_d_h); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", milliseconds_h_d); printf("Elapsed time kernel: %.10f milliseconds\n", milliseconds); printf("Elapsed time Device->Host: %.10f milliseconds\n", milliseconds_d_h); @@ -172,9 +170,8 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo void clean(GraficCommon* device_object){ GraficObject* deviceObj = static_cast(device_object); - hipError_t err = hipSuccess; - err = hipFree(deviceObj->d_A); - + + hipError_t err = hipFree(deviceObj->d_A); if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector A (error code %s)!\n", hipGetErrorString(err)); @@ -182,14 +179,25 @@ void clean(GraficCommon* device_object){ } err = hipFree(deviceObj->d_B); - if (err != hipSuccess) { fprintf(stderr, "Failed to free device vector B (error code %s)!\n", hipGetErrorString(err)); return; } + err = hipFree(deviceObj->low_filter); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to free device vector low_filter (error code %s)!\n", hipGetErrorString(err)); + return; + } + err = hipFree(deviceObj->high_filter); + if (err != hipSuccess) + { + fprintf(stderr, "Failed to free device vector high_filter (error code %s)!\n", hipGetErrorString(err)); + return; + } // delete events delete deviceObj->start; diff --git a/gpu4s_benchmark/wavelet_transform/hip/hip_common.h b/gpu4s_benchmark/wavelet_transform/hip/hip_common.h deleted file mode 100644 index 7347e7fa..00000000 --- a/gpu4s_benchmark/wavelet_transform/hip/hip_common.h +++ /dev/null @@ -1,16 +0,0 @@ -/** * ==================================================================== - * @file hip_common.h (./wavelet_transform) - * @brief Shared declarations, data structures, and timing utilities - * for HIP benchmark backends. - * @paragraph License - * ESA-PL Strong Copyleft – v2.5 - * ======================================================================= */ -#pragma once - -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif \ No newline at end of file diff --git a/gpu4s_benchmark/wavelet_transform/hip/lib_hip.cpp b/gpu4s_benchmark/wavelet_transform/hip/lib_hip.cpp index 647d2ea6..f4c774df 100644 --- a/gpu4s_benchmark/wavelet_transform/hip/lib_hip.cpp +++ b/gpu4s_benchmark/wavelet_transform/hip/lib_hip.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -116,24 +115,26 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ GraficObject* deviceObj = static_cast(device_object); dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); dim3 dimGrid(ceil(float(n)/dimBlock.x)); + // kernel time execution + Clock kernelCLK; - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif - + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); + #ifdef INT - hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); - hipLaunchKernelGGL((wavelet_transform_low), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); + hipLaunchKernelGGL((wavelet_transform_low), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n); #else - hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); + hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, 0, deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); #endif + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } diff --git a/gpu4s_benchmark/wavelet_transform/hip/lib_hip_opt.cpp b/gpu4s_benchmark/wavelet_transform/hip/lib_hip_opt.cpp index 7193c790..cbbc9eba 100644 --- a/gpu4s_benchmark/wavelet_transform/hip/lib_hip_opt.cpp +++ b/gpu4s_benchmark/wavelet_transform/hip/lib_hip_opt.cpp @@ -1,4 +1,3 @@ -#include "hip/hip_runtime.h" #include "../benchmark_library.h" @@ -156,49 +155,46 @@ wavelet_transform(const bench_t *A, bench_t *B, const int n, const bench_t *lowp #endif void execute_kernel(GraficCommon* device_object, unsigned int n){ - -GraficObject* deviceObj = static_cast(device_object); - - - #ifdef PROFILING_CLOCK - kernelCLK.start(); - #endif + GraficObject* deviceObj = static_cast(device_object); + // kernel time execution + Clock kernelCLK; + // profilling start + kernelCLK.start(); (void)hipEventRecord(*deviceObj->start); - #ifdef INT - - dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); - dim3 dimGrid(ceil(float(n/NUMBERSUBDIVISIONS)/dimBlock.x)); - - hipStream_t cuda_streams[NUMBERSUBDIVISIONS]; - for (unsigned int streams = 0; streams < NUMBERSUBDIVISIONS; ++streams) - { - hipStreamCreate(&cuda_streams[streams]); - } - - for (unsigned int iter = 0; iter < NUMBERSUBDIVISIONS; ++iter) - { - hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[iter], deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); - hipLaunchKernelGGL((wavelet_transform_low), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[iter], deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); - } - + #ifdef INT + dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); + dim3 dimGrid(ceil(float(n/NUMBERSUBDIVISIONS)/dimBlock.x)); + + hipStream_t cuda_streams[NUMBERSUBDIVISIONS]; + for (unsigned int streams = 0; streams < NUMBERSUBDIVISIONS; ++streams) + { + hipStreamCreate(&cuda_streams[streams]); + } + + for (unsigned int iter = 0; iter < NUMBERSUBDIVISIONS; ++iter) + { + hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[iter], deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); + hipLaunchKernelGGL((wavelet_transform_low), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[iter], deviceObj->d_A, deviceObj->d_B, n, iter,dimGrid.x); + } #else - hipStream_t cuda_streams[2]; - dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); - dim3 dimGrid(ceil(float(n)/dimBlock.x)); - for (unsigned int streams = 0; streams < 2; ++streams) - { - (void)hipStreamCreate(&cuda_streams[streams]); - } - hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[0], deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); - hipLaunchKernelGGL((wavelet_transform_high), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[1], deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); + hipStream_t cuda_streams[2]; + dim3 dimBlock(BLOCK_SIZE*BLOCK_SIZE); + dim3 dimGrid(ceil(float(n)/dimBlock.x)); + for (unsigned int streams = 0; streams < 2; ++streams) + { + (void)hipStreamCreate(&cuda_streams[streams]); + } + hipLaunchKernelGGL((wavelet_transform), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[0], deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); + hipLaunchKernelGGL((wavelet_transform_high), dim3(dimGrid), dim3(dimBlock), 0, cuda_streams[1], deviceObj->d_A, deviceObj->d_B, n, deviceObj->low_filter, deviceObj->high_filter); #endif + // profilling end (void)hipEventRecord(*deviceObj->stop); + hipDeviceSynchronize(); + kernelCLK.end(); - #ifdef PROFILING_CLOCK - hipDeviceSynchronize(); - kernelCLK.end(); - #endif + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedMS(); } \ No newline at end of file diff --git a/gpu4s_benchmark/wavelet_transform/main.cpp b/gpu4s_benchmark/wavelet_transform/main.cpp index b545a086..49c0817f 100644 --- a/gpu4s_benchmark/wavelet_transform/main.cpp +++ b/gpu4s_benchmark/wavelet_transform/main.cpp @@ -111,6 +111,14 @@ int main(int argc, char *argv[]){ printf("Using device: %s\n", device); } + // Update profiling clock mode + wavelet_bench->profiling_clock = arguments_parameters->profiling_clock; + + / If android and opencl force profiling clock + #ifdef PROFILING_CLOCK + wavelet_bench->profiling_clock = true; + #endif + // init memory device_memory_init(wavelet_bench, arguments_parameters->size , arguments_parameters->size ); // copy memory to device @@ -199,7 +207,6 @@ return 0; // Arguments part - void print_usage(const char * appName) { printf("Usage: %s -s Size -k [-v] [-e] [-o] [-t] [-d] [-i input_file_A_MATRIX input_file_B_MATRIX] \n", appName); @@ -216,6 +223,7 @@ void print_usage(const char * appName) printf(" -d: selects GPU\n"); printf(" -f: mutes all print\n"); printf(" -h: print help information\n"); + printf(" -p: clock profilling \n"); } void init_arguments(BenchmarkParameters* arguments_parameters){ @@ -230,6 +238,7 @@ void init_arguments(BenchmarkParameters* arguments_parameters){ arguments_parameters->csv_format = false; arguments_parameters->mute_messages = false; arguments_parameters->csv_format_timestamp = false; + arguments_parameters->profiling_clock = false; } int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_parameters){ @@ -260,6 +269,7 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par strcpy(arguments_parameters->input_file_B,argv[args]); break; case 's' : args +=1; arguments_parameters->size = atoi(argv[args]);break; + case 'p' : arguments_parameters->profiling_clock = true;break; default: print_usage(argv[0]); return ERROR_ARGUMENTS; } @@ -273,4 +283,4 @@ int arguments_handler(int argc, char ** argv, BenchmarkParameters* arguments_par arguments_parameters->csv_format = false; } return OK_ARGUMENTS; -} \ No newline at end of file +} diff --git a/gpu4s_benchmark/wavelet_transform/opencl/lib_opencl.cpp b/gpu4s_benchmark/wavelet_transform/opencl/lib_opencl.cpp index 2de53019..86f7c557 100644 --- a/gpu4s_benchmark/wavelet_transform/opencl/lib_opencl.cpp +++ b/gpu4s_benchmark/wavelet_transform/opencl/lib_opencl.cpp @@ -8,15 +8,6 @@ #include "GEN_kernel.hcl" #endif -#ifdef PROFILING_CLOCK - // kernel time execution - Clock kernelCLK; - // host <-> device - Clock h2dCLK; - Clock d2hCLK; -#endif - -//#define BLOCK_SIZE 16 void init(GraficCommon* device_object, char* device_name){ init(device_object, 0,0, device_name); } @@ -80,11 +71,11 @@ bool device_memory_init(GraficCommon* device_object, unsigned int size_a_matrix, void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned int size_a){ GraficObject* deviceObj = static_cast(device_object); - // copy memory host -> device + // host -> device + Clock h2dCLK; - #ifdef PROFILING_CLOCK - h2dCLK.start(); - #endif + // Clock profilling start + h2dCLK.start(); cl_int err = deviceObj->queue->enqueueWriteBuffer(*deviceObj->d_A,CL_TRUE,0,sizeof(bench_t)*size_a, h_A, NULL, deviceObj->evt_copyA); if (err != CL_SUCCESS) @@ -111,10 +102,11 @@ void copy_memory_to_device(GraficCommon* device_object, bench_t* h_A, unsigned i } #endif - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - h2dCLK.end(); - #endif + // Clock profilling end + h2dCLK.end(); + + // store the hd2h time + deviceObj->h2d_elapsed_time = h2dCLK.getElapsedNS(); } @@ -147,10 +139,11 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ exit(1); } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); // Clear queue to ensure accurate start - kernelCLK.start(); - #endif + // kernel time execution + Clock kernelCLK; + + // Clock profilling start + kernelCLK.start(); #ifdef INT @@ -185,53 +178,63 @@ void execute_kernel(GraficCommon* device_object, unsigned int n){ #endif - #ifdef PROFILING_CLOCK - kernelCLK.end(); - #endif + // Wait for completion before stopping the clock + deviceObj->queue->finish(); + // Clock profilling end + kernelCLK.end(); + + // store the kernel time + deviceObj->elapsed_time = kernelCLK.getElapsedNS(); } void copy_memory_to_host(GraficCommon* device_object, bench_t* h_C, int size){ GraficObject* deviceObj = static_cast(device_object); - #ifdef PROFILING_CLOCK - d2hCLK.start(); - #endif + // device -> host + Clock d2hCLK; + + // Clock profilling start + d2hCLK.start(); - deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B,CL_TRUE,0,sizeof(bench_t)*size,h_C, NULL, deviceObj->evt_copyC); + cl_int err = deviceObj->queue->enqueueReadBuffer(*deviceObj->d_B, CL_TRUE, 0, sizeof(bench_t)*size, h_C, NULL, deviceObj->evt_copyC); + if (err != CL_SUCCESS) + { + fprintf(stderr, "Failed to copy vector B from device to host (OpenCL error code %d)!\n", err); + return; + } - #ifdef PROFILING_CLOCK - deviceObj->queue->finish(); - d2hCLK.end(); - #endif + // Clock profilling end + d2hCLK.end(); + + // store the hd2h time + deviceObj->d2h_elapsed_time = d2hCLK.getElapsedNS(); } float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_format_timestamp, long int current_time){ GraficObject* deviceObj = static_cast(device_object); deviceObj->evt_copyC->wait(); + float elapsed_h_d = 0, elapsed = 0, elapsed_d_h = 0; - elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); - elapsed_h_d += deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); - - elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); - #ifdef INT - elapsed += deviceObj->evt_int->getProfilingInfo() - deviceObj->evt_int->getProfilingInfo(); - #endif - //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); - - elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); - //printf("Elapsed time Device->Host: %.10f \n", ); - - #ifdef PROFILING_CLOCK + if (deviceObj->profiling_clock) + { // --- FIX: Use instead of CLBlast event profiling (unreliable on PROFILING_CLOCK) --- - elapsed_h_d = h2dCLK.getElapsedNS(); - elapsed = kernelCLK.getElapsedNS(); - elapsed_d_h = d2hCLK.getElapsedNS(); - const char* profilingMode = "CLOCK"; - #else - const char* profilingMode = "GPU"; - #endif + elapsed_h_d = deviceObj->h2d_elapsed_time; + elapsed = deviceObj->elapsed_time; + elapsed_d_h = deviceObj->d2h_elapsed_time; + }else{ + + elapsed_h_d = deviceObj->evt_copyA->getProfilingInfo() - deviceObj->evt_copyA->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyB->getProfilingInfo() - deviceObj->evt_copyB->getProfilingInfo(); + elapsed_h_d += deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Host->Device: %.10f \n", elapsed / 1000000.0); + + elapsed = deviceObj->evt->getProfilingInfo() - deviceObj->evt->getProfilingInfo(); + elapsed += deviceObj->evt_int->getProfilingInfo() - deviceObj->evt_int->getProfilingInfo(); + //printf("Elapsed time kernel: %.10f \n", elapsed / 1000000.0); + + elapsed_d_h = deviceObj->evt_copyC->getProfilingInfo() - deviceObj->evt_copyC->getProfilingInfo(); + //printf("Elapsed time Device->Host: %.10f \n", ); + } if (csv_format_timestamp){ printf("%.10f;%.10f;%.10f;%ld;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0, current_time); @@ -239,7 +242,7 @@ float get_elapsed_time(GraficCommon* device_object, bool csv_format, bool csv_fo else if (csv_format){ printf("%.10f;%.10f;%.10f;\n", elapsed_h_d / 1000000.0,elapsed / 1000000.0,elapsed_d_h / 1000000.0); }else{ - printf("profiling mode: %s\n", profilingMode); + printf("profiling mode: %s\n", deviceObj->profiling_clock ? "CLOCK" : "FALSE"); printf("Elapsed time Host->Device: %.10f milliseconds\n", (elapsed_h_d / 1000000.0)); printf("Elapsed time kernel: %.10f milliseconds\n", elapsed / 1000000.0); printf("Elapsed time Device->Host: %.10f milliseconds\n", elapsed_d_h / 1000000.0);