diff --git a/samples/2_Cookbook/13_occupancy/Makefile b/samples/2_Cookbook/13_occupancy/Makefile new file mode 100644 index 0000000000..3ec069d9e6 --- /dev/null +++ b/samples/2_Cookbook/13_occupancy/Makefile @@ -0,0 +1,20 @@ +HIP_PATH?= $(wildcard /opt/rocm/hip) +ifeq (,$(HIP_PATH)) + HIP_PATH=../../.. +endif +HIPCC=$(HIP_PATH)/bin/hipcc + +EXE=./occupancy + +.PHONY: test + +all: test + +$(EXE): occupancy.cpp + $(HIPCC) $^ -o $@ + +test: $(EXE) + $(EXE) + +clean: + rm -f *.o $(EXE) diff --git a/samples/2_Cookbook/13_occupancy/occupancy.cpp b/samples/2_Cookbook/13_occupancy/occupancy.cpp new file mode 100644 index 0000000000..a9f4e198b0 --- /dev/null +++ b/samples/2_Cookbook/13_occupancy/occupancy.cpp @@ -0,0 +1,176 @@ +/* +Copyright (c) 2015-Present 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 WARRANNTY OF ANY KIND, EXPRESS OR +IMPLIED, INNCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANNY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "hip/hip_runtime.h" +#include +#define NUM 1000000 + +#define HIP_CHECK(status) \ + if (status != hipSuccess) { \ + std::cout << "Got Status: " << status << " at Line: " << __LINE__ << std::endl; \ + exit(0); \ + } + +// Device (Kernel) function +__global__ void multiply(float* C, float* A, float* B, int N){ + + int tx = blockDim.x*blockIdx.x+threadIdx.x; + + if (tx < N){ + C[tx] = A[tx] * B[tx]; + } +} +// CPU implementation +void multiplyCPU(float* C, float* A, float* B, int N){ + + for(unsigned int i=0; i eps) { + errors++; + } + } + + if (errors != 0){ + printf("\nManual Test FAILED: %d errors\n", errors); + errors=0; + } else { + printf("\nManual Test PASSED!\n"); + } + + for (i = 0; i < NUM; i++) { + if (std::abs(C1[i] - cpuC[i]) > eps) { + errors++; + } + } + + if (errors != 0){ + printf("\n Automatic Test FAILED: %d errors\n", errors); + } else { + printf("\nAutomatic Test PASSED!\n"); + } + + HIP_CHECK(hipFree(Ad)); + HIP_CHECK(hipFree(Bd)); + HIP_CHECK(hipFree(C0d)); + HIP_CHECK(hipFree(C1d)); + + free(A); + free(B); + free(C0); + free(C1); + free(cpuC); +}