diff --git a/Exercises/CMakeLists.txt b/Exercises/CMakeLists.txt new file mode 100644 index 0000000..4845623 --- /dev/null +++ b/Exercises/CMakeLists.txt @@ -0,0 +1,87 @@ +cmake_minimum_required(VERSION 3.6) + +# Copyright (C) 2018 Paul Keir, University of the West of Scotland + +project(HOCL-Exercises) + +find_package(OpenCL REQUIRED) +find_package(OpenMP REQUIRED) + +set_property(GLOBAL PROPERTY USE_FOLDERS ON) +set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CMakePredefinedTargets") + +include_directories(${OpenCL_INCLUDE_DIRS} C_common Cpp_common) +link_libraries(${OpenCL_LIBRARY}) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + +set(name Exercise01) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/DeviceInfo.cpp) +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +set(name Exercise03) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/vadd.cpp ${name}/Cpp/vadd.cl) +target_compile_definitions(${tname} PRIVATE EX3_VADD_CL_PATH="../${name}/Cpp/vadd.cl") +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +set(name Exercise04) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/vadd.cpp ${name}/Cpp/vadd.cl) +target_compile_definitions(${tname} PRIVATE EX4_VADD_CL_PATH="../${name}/Cpp/vadd.cl") +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +set(name Exercise05) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/vadd.cpp ${name}/Cpp/vadd.cl) +target_compile_definitions(${tname} PRIVATE EX5_VADD_CL_PATH="../${name}/Cpp/vadd.cl") +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +set(name Exercise06) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/matmul.cpp ${name}/Cpp/matmul.hpp ${name}/Cpp/matrix_lib.cpp ${name}/Cpp/matrix_lib.hpp) +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +set(name Exercise09) +set(tname ${name}-Cpp) +add_executable(${tname} ${name}/Cpp/pi.cpp) +set_target_properties(${tname} PROPERTIES FOLDER Cpp) + +############################ + +set(name Exercise01) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/DeviceInfo.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise02) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/vadd_c.c C_common/wtime.c C_common/device_info.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise04) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/vadd_c.c C_common/wtime.c C_common/device_info.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise05) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/vadd_c.c C_common/wtime.c C_common/device_info.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise06) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/matmul.c ${name}/C/matmul.h ${name}/C/matrix_lib.c ${name}/C/matrix_lib.h C_common/wtime.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise09) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/pi.c C_common/wtime.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +set(name Exercise13) +set(tname ${name}-C) +add_executable(${tname} ${name}/C/gameoflife.c) +set_target_properties(${tname} PROPERTIES FOLDER C) + +#set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT Exercise01-Cpp) diff --git a/Exercises/Exercise01/C/DeviceInfo.c b/Exercises/Exercise01/C/DeviceInfo.c index 147fe34..f3922f5 100644 --- a/Exercises/Exercise01/C/DeviceInfo.c +++ b/Exercises/Exercise01/C/DeviceInfo.c @@ -34,7 +34,7 @@ int main(void) return EXIT_FAILURE; } // Create a list of platform IDs - cl_platform_id platform[num_platforms]; + cl_platform_id *platform = malloc(num_platforms * sizeof(cl_platform_id)); err = clGetPlatformIDs(num_platforms, platform, NULL); checkError(err, "Getting platforms"); @@ -66,7 +66,7 @@ int main(void) checkError(err, "Finding devices"); // Get the device IDs - cl_device_id device[num_devices]; + cl_device_id *device = malloc(num_devices * sizeof(cl_device_id)); err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_ALL, num_devices, device, NULL); checkError(err, "Getting devices"); printf("Number of devices: %d\n", num_devices); @@ -118,22 +118,25 @@ int main(void) err = clGetDeviceInfo(device[j], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &num, NULL); checkError(err, "Getting device max work-item dims"); // Get the max. dimensions of the work-groups - size_t dims[num]; - err = clGetDeviceInfo(device[j], CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(dims), &dims, NULL); + size_t *dims = malloc(num * sizeof(size_t)); + err = clGetDeviceInfo(device[j], CL_DEVICE_MAX_WORK_ITEM_SIZES, num * sizeof(size_t), dims, NULL); checkError(err, "Getting device max work-item sizes"); printf("\t\tMax Work-group Dims: ( "); for (size_t k = 0; k < num; k++) { printf("%ld ", dims[k]); } - printf(")\n"); + free(dims); + printf(")\n"); printf("\t-------------------------\n"); } + free(device); printf("\n-------------------------\n"); } + free(platform); return EXIT_SUCCESS; } diff --git a/Exercises/Exercise01/Cpp/DeviceInfo.cpp b/Exercises/Exercise01/Cpp/DeviceInfo.cpp index a4341b8..9ca55a7 100644 --- a/Exercises/Exercise01/Cpp/DeviceInfo.cpp +++ b/Exercises/Exercise01/Cpp/DeviceInfo.cpp @@ -10,7 +10,7 @@ #define __CL_ENABLE_EXCEPTIONS -#include "cl.hpp" +#include "CL/cl.hpp" #include #include diff --git a/Exercises/Exercise02/C/vadd_c.c b/Exercises/Exercise02/C/vadd_c.c index c5c07df..acff671 100644 --- a/Exercises/Exercise02/C/vadd_c.c +++ b/Exercises/Exercise02/C/vadd_c.c @@ -15,6 +15,7 @@ #include #include #include + #ifdef __APPLE__ #include #include @@ -111,7 +112,7 @@ int main(int argc, char** argv) } // Get all platforms - cl_platform_id Platform[numPlatforms]; + cl_platform_id *Platform = malloc(numPlatforms * sizeof(cl_platform_id)); err = clGetPlatformIDs(numPlatforms, Platform, NULL); checkError(err, "Getting platforms"); @@ -125,6 +126,8 @@ int main(int argc, char** argv) } } + free(Platform); + if (device_id == NULL) checkError(err, "Finding a device"); diff --git a/Exercises/Exercise03/Cpp/vadd.cpp b/Exercises/Exercise03/Cpp/vadd.cpp index 8128fe8..48fc77f 100644 --- a/Exercises/Exercise03/Cpp/vadd.cpp +++ b/Exercises/Exercise03/Cpp/vadd.cpp @@ -15,7 +15,7 @@ #define __CL_ENABLE_EXCEPTIONS -#include "cl.hpp" +#include "CL/cl.hpp" #include "util.hpp" // utility library @@ -34,6 +34,11 @@ #define DEVICE CL_DEVICE_TYPE_DEFAULT #endif +// Set the filepath if not already set +#ifndef EX3_VADD_CL_PATH +#define EX3_VADD_CL_PATH "vadd.cl" +#endif + //------------------------------------------------------------------------------ #define TOL (0.001) // tolerance used in floating point comparisons @@ -64,7 +69,7 @@ int main(void) // Load in kernel source, creating a program object for the context - cl::Program program(context, util::loadProgram("vadd.cl"), true); + cl::Program program(context, util::loadProgram(EX3_VADD_CL_PATH), true); // Get the command queue cl::CommandQueue queue(context); diff --git a/Exercises/Exercise04/C/vadd_c.c b/Exercises/Exercise04/C/vadd_c.c index c5c07df..34cfd75 100644 --- a/Exercises/Exercise04/C/vadd_c.c +++ b/Exercises/Exercise04/C/vadd_c.c @@ -111,7 +111,7 @@ int main(int argc, char** argv) } // Get all platforms - cl_platform_id Platform[numPlatforms]; + cl_platform_id *Platform = malloc(numPlatforms * sizeof(cl_platform_id)); err = clGetPlatformIDs(numPlatforms, Platform, NULL); checkError(err, "Getting platforms"); @@ -125,6 +125,8 @@ int main(int argc, char** argv) } } + free(Platform); + if (device_id == NULL) checkError(err, "Finding a device"); diff --git a/Exercises/Exercise04/Cpp/vadd.cpp b/Exercises/Exercise04/Cpp/vadd.cpp index e6fabdf..d794d95 100644 --- a/Exercises/Exercise04/Cpp/vadd.cpp +++ b/Exercises/Exercise04/Cpp/vadd.cpp @@ -15,7 +15,7 @@ #define __CL_ENABLE_EXCEPTIONS -#include "cl.hpp" +#include "CL/cl.hpp" #include "util.hpp" // utility library @@ -32,6 +32,11 @@ #define DEVICE CL_DEVICE_TYPE_DEFAULT #endif +// Set the filepath if not already set +#ifndef EX4_VADD_CL_PATH +#define EX4_VADD_CL_PATH "vadd.cl" +#endif + #include //------------------------------------------------------------------------------ @@ -64,7 +69,7 @@ int main(void) // Load in kernel source, creating a program object for the context - cl::Program program(context, util::loadProgram("vadd.cl"), true); + cl::Program program(context, util::loadProgram(EX4_VADD_CL_PATH), true); // Get the command queue cl::CommandQueue queue(context); diff --git a/Exercises/Exercise05/C/vadd_c.c b/Exercises/Exercise05/C/vadd_c.c index c5c07df..34cfd75 100644 --- a/Exercises/Exercise05/C/vadd_c.c +++ b/Exercises/Exercise05/C/vadd_c.c @@ -111,7 +111,7 @@ int main(int argc, char** argv) } // Get all platforms - cl_platform_id Platform[numPlatforms]; + cl_platform_id *Platform = malloc(numPlatforms * sizeof(cl_platform_id)); err = clGetPlatformIDs(numPlatforms, Platform, NULL); checkError(err, "Getting platforms"); @@ -125,6 +125,8 @@ int main(int argc, char** argv) } } + free(Platform); + if (device_id == NULL) checkError(err, "Finding a device"); diff --git a/Exercises/Exercise05/Cpp/vadd.cpp b/Exercises/Exercise05/Cpp/vadd.cpp index 8128fe8..9c0f565 100644 --- a/Exercises/Exercise05/Cpp/vadd.cpp +++ b/Exercises/Exercise05/Cpp/vadd.cpp @@ -15,7 +15,7 @@ #define __CL_ENABLE_EXCEPTIONS -#include "cl.hpp" +#include "CL/cl.hpp" #include "util.hpp" // utility library @@ -34,6 +34,11 @@ #define DEVICE CL_DEVICE_TYPE_DEFAULT #endif +// Set the filepath if not already set +#ifndef EX5_VADD_CL_PATH +#define EX5_VADD_CL_PATH "vadd.cl" +#endif + //------------------------------------------------------------------------------ #define TOL (0.001) // tolerance used in floating point comparisons @@ -64,7 +69,7 @@ int main(void) // Load in kernel source, creating a program object for the context - cl::Program program(context, util::loadProgram("vadd.cl"), true); + cl::Program program(context, util::loadProgram(EX5_VADD_CL_PATH), true); // Get the command queue cl::CommandQueue queue(context); diff --git a/Exercises/Exercise13/C/gameoflife.c b/Exercises/Exercise13/C/gameoflife.c index fe3a3a2..e936de9 100644 --- a/Exercises/Exercise13/C/gameoflife.c +++ b/Exercises/Exercise13/C/gameoflife.c @@ -44,9 +44,9 @@ void accelerate_life(const char* tick, char* tock, const int nx, const int ny) // wrapping around if required unsigned int x_l, x_r, y_u, y_d; - unsigned int j; + int i, j; #pragma omp parallel for private(j, idx, x_l, x_r, y_u, y_d) - for (unsigned int i = 0; i < ny; i++) + for (i = 0; i < ny; i++) { for (j = 0; j < nx; j++) { @@ -101,11 +101,11 @@ void accelerate_life(const char* tick, char* tock, const int nx, const int ny) int main(int argc, char **argv) { - // Check we have a starting state file if (argc != 3) { printf("Usage:\n./gameoflife input.dat input.params\n"); + // ./gameoflife ../Exercise13/Examples/Acorn/acorn.dat ../Exercise13/Examples/Acorn/input.params return EXIT_FAILURE; } diff --git a/Exercises/Exercise13/README.md b/Exercises/Exercise13/README.md index 71ee631..496eb27 100644 --- a/Exercises/Exercise13/README.md +++ b/Exercises/Exercise13/README.md @@ -8,8 +8,8 @@ Goal Procedure --------- * Examine the CUDA kernel and identify which parts need changing - * Change them to the OpenCL equivalents -* Examine the Host code and part the commands to the OpenCL equivalents +* Change them to the OpenCL equivalents +* Examine the Host code and port the commands to the OpenCL equivalents Expected output --------------- diff --git a/Solutions/C_common/device_info.c b/Solutions/C_common/device_info.c index 8a14e87..7794fb5 100644 --- a/Solutions/C_common/device_info.c +++ b/Solutions/C_common/device_info.c @@ -15,7 +15,6 @@ #include #include #ifdef __APPLE__ - #include #else #include