diff --git a/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/CMakeLists.txt b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/CMakeLists.txt new file mode 100644 index 0000000000..bed43ff8ec --- /dev/null +++ b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (C) 2022 Advanced Micro Devices, Inc. All Rights Reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +cmake_minimum_required(VERSION 3.21.3) + +project(cmake_hip_lang_support VERSION 1.0 + DESCRIPTION "HIP Language Support with upstream CMake" + LANGUAGES HIP) +# Create the executable +add_executable(square square.hip) diff --git a/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/README.md b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/README.md new file mode 100644 index 0000000000..c770d75f63 --- /dev/null +++ b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/README.md @@ -0,0 +1,16 @@ +### This will test HIP language support in upstream CMake +I. Build +mkdir -p build; cd build +rm -rf *; cmake .. +make + +II. Test +$ ./square +info: running on device +info: allocate host mem ( 7.63 MB) +info: allocate device mem ( 7.63 MB) +info: copy Host2Device +info: launch 'vector_square' kernel +info: copy Device2Host +info: check result +PASSED! diff --git a/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/square.hip b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/square.hip new file mode 100644 index 0000000000..7cee90818a --- /dev/null +++ b/projects/hip-tests/samples/2_Cookbook/22_cmake_hip_lang/square.hip @@ -0,0 +1,98 @@ +/* +Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include + +#define CHECK(cmd) \ +{\ + hipError_t error = cmd;\ + if (error != hipSuccess) { \ + fprintf(stderr, "error: '%s'(%d) at %s:%d\n", hipGetErrorString(error), error,__FILE__, __LINE__); \ + exit(EXIT_FAILURE);\ + }\ +} + + +/* + * Square each element in the array A and write to array C. + */ +template +__global__ void +vector_square(T *C_d, T *A_d, size_t N) +{ + size_t offset = (blockIdx.x * blockDim.x + threadIdx.x); + size_t stride = blockDim.x * gridDim.x ; + + for (size_t i=offset; i