From b97e94b8ed1c7244e8bd98345f570a10ce0d1202 Mon Sep 17 00:00:00 2001 From: Hansung Kim Date: Mon, 25 Sep 2023 13:29:00 -0700 Subject: [PATCH] [tests] vecadd|sgemm|saxpy: save input buffers to file --- tests/opencl/saxpy/main.cc | 24 ++++++++++++++++++++++++ tests/opencl/sgemm/main.cc | 25 +++++++++++++++++++++++++ tests/opencl/vecadd/main.cc | 25 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/tests/opencl/saxpy/main.cc b/tests/opencl/saxpy/main.cc index afe297ea..e5cf866f 100644 --- a/tests/opencl/saxpy/main.cc +++ b/tests/opencl/saxpy/main.cc @@ -78,6 +78,25 @@ static int read_kernel_file(const char* filename, uint8_t** data, size_t* size) return 0; } +static int write_operand_file(const char* filename, void* data, size_t size) { + if (nullptr == filename || nullptr == data || 0 == size) + return -1; + + FILE* fp = fopen(filename, "wb"); + if (NULL == fp) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + size_t wsize = fwrite(data, size, 1, fp); + if (wsize != 1) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + return 0; +} + uint8_t *kernel_bin = NULL; /// @@ -209,6 +228,11 @@ int main(int argc, char **argv) { for (int i = 0; i < size; i++) { h_src[i] = ((float)rand() / (float)(RAND_MAX)) * 100.0; } + + // NOTE(hansung): Dump operand buffer to a file + if (write_operand_file("saxpy.input.src.bin", h_src, nbytes) != 0) + return EXIT_FAILURE; + CL_CHECK(clEnqueueWriteBuffer(queue, input_buffer, CL_TRUE, 0, nbytes, h_src, 0, NULL, NULL)); free(h_src); diff --git a/tests/opencl/sgemm/main.cc b/tests/opencl/sgemm/main.cc index 06893876..95c4eccf 100644 --- a/tests/opencl/sgemm/main.cc +++ b/tests/opencl/sgemm/main.cc @@ -52,6 +52,25 @@ static int read_kernel_file(const char* filename, uint8_t** data, size_t* size) return 0; } +static int write_operand_file(const char* filename, void* data, size_t size) { + if (nullptr == filename || nullptr == data || 0 == size) + return -1; + + FILE* fp = fopen(filename, "wb"); + if (NULL == fp) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + size_t wsize = fwrite(data, size, 1, fp); + if (wsize != 1) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + return 0; +} + static void matmul(float *C, const float* A, const float *B, int M, int N, int K) { for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { @@ -194,6 +213,12 @@ int main (int argc, char **argv) { //printf("*** [%d]: h_a=%f, h_b=%f\n", i, h_a[i], h_b[i]); } + // NOTE(hansung): Dump operand buffer to a file + if (write_operand_file("sgemm.input.a.bin", h_a, nbytes) != 0) + return EXIT_FAILURE; + if (write_operand_file("sgemm.input.b.bin", h_b, nbytes) != 0) + return EXIT_FAILURE; + // Creating command queue commandQueue = CL_CHECK2(clCreateCommandQueue(context, device_id, 0, &_err)); diff --git a/tests/opencl/vecadd/main.cc b/tests/opencl/vecadd/main.cc index 1bf8774d..9acf545d 100644 --- a/tests/opencl/vecadd/main.cc +++ b/tests/opencl/vecadd/main.cc @@ -52,6 +52,25 @@ static int read_kernel_file(const char* filename, uint8_t** data, size_t* size) return 0; } +static int write_operand_file(const char* filename, void* data, size_t size) { + if (nullptr == filename || nullptr == data || 0 == size) + return -1; + + FILE* fp = fopen(filename, "wb"); + if (NULL == fp) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + size_t wsize = fwrite(data, size, 1, fp); + if (wsize != 1) { + fprintf(stderr, "Failed to write operand data.\n"); + return -1; + } + + return 0; +} + static bool almost_equal(float a, float b, int ulp = 4) { union fi_t { int i; float f; }; fi_t fa, fb; @@ -175,6 +194,12 @@ int main (int argc, char **argv) { //printf("*** [%d]: h_a=%f, h_b=%f\n", i, h_a[i], h_b[i]); } + // NOTE(hansung): Dump operand buffer to a file + if (write_operand_file("vecadd.input.a.bin", h_a, nbytes) != 0) + return EXIT_FAILURE; + if (write_operand_file("vecadd.input.b.bin", h_b, nbytes) != 0) + return EXIT_FAILURE; + // Creating command queue // NOTE(hansung): The 3rd properties arg is a bit-field, where fields like // CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE can be set. With value of 0,