diff --git a/src/backend/tests/kernelprog/ROC_M/main_hip.c b/src/backend/tests/kernelprog/ROC_M/main_hip.c new file mode 100644 index 0000000..675839f --- /dev/null +++ b/src/backend/tests/kernelprog/ROC_M/main_hip.c @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include +#include + +#define HIP_CHECK(condition) \ +{ \ + const hipError_t error = condition; \ + if (error != hipSuccess) \ + { \ + fprintf(stderr, "An error encountered: \"%s\" at %s:%d\n", \ + hipGetErrorString(error), __FILE__, __LINE__); \ + exit(EXIT_FAILURE); \ + } \ +} + +#define CHECK_RET_CODE(call, ret_code) \ + { \ + if ((call) != ret_code) { \ + printf("Failed in call: %s", #call ) ; \ + } \ + } + #define HIPRTC_CHECK(call) CHECK_RET_CODE(call, HIPRTC_SUCCESS) + + static char * arch_name(hipDeviceProp_t props){ + if(props.gcnArchName[0]) + { + static char *new_str = NULL ; + if((new_str = (char *)malloc(strlen("--gpu-architecture=") + strlen(props.gcnArchName) + 1)) != NULL){ + new_str[0] = '\0'; + strcat(new_str, "--gpu-architecture="); + strcat(new_str, props.gcnArchName); + + return new_str ; + } + + else{ + printf( "malloc failed") ; + return new_str; + } + + } + +} + +char str[] = " extern \"C\" __global__ void saxpy_kernel(const float a, const float * d_x, float * d_y, const unsigned int size)\n" +"{\n" +" const unsigned int global_idx = blockIdx.x * blockDim.x + threadIdx.x;\n" +" if(global_idx < size)\n" +" {\n" +" d_y[global_idx] = a * d_x[global_idx] + d_y[global_idx];\n" +" }\n" +"}"; + +int ceiling_div(int size, int block_size){ + // return (size/block_size) ; + return (size + block_size - 1) / block_size; +} + +int main(){ + // program to be compiled in runtime. + hiprtcProgram prog; + + // create program. + char * const_str = str ; + hiprtcResult result = hiprtcCreateProgram(&prog, + const_str, + "saxpy_kernel.c", + 0, + NULL, + NULL + ); + if(result!= HIPRTC_SUCCESS){ + printf("FAILED. hiprtcCreateProgram"); + } + + printf("created program") ; + + // Get device properties from the first device available. + hipDeviceProp_t props; + unsigned int device_id = 0; + HIP_CHECK(hipGetDeviceProperties(&props, device_id)); + const char * arch = arch_name(props) ; + printf("\n%s",arch); + result = hiprtcCompileProgram(prog,1, &arch ) ; + // free(arch); + if(result!= HIPRTC_SUCCESS){ + printf("FAILED hiprtcCompileProgram."); + } + + // Get the size of the log (possibly) generated during compilation. + size_t log_size; + HIPRTC_CHECK( hiprtcGetProgramLogSize(prog, &log_size)); + + if(log_size){ + char log[log_size]; + for(int i = 0; i