diff --git a/tests/C-tests/CMakeLists.txt b/tests/C-tests/CMakeLists.txt index 5e6aef7d..7e5459df 100644 --- a/tests/C-tests/CMakeLists.txt +++ b/tests/C-tests/CMakeLists.txt @@ -11,6 +11,17 @@ if(BML_OMP_OFFLOAD) ${CMAKE_CURRENT_BINARY_DIR}/test-openmp_offload) endif() +if(BML_MAGMA) + add_executable(test-magma test_magma.c) + target_link_libraries(test-magma ${LINK_LIBRARIES}) + set_target_properties(test-magma + PROPERTIES + COMPILE_FLAGS ${OpenMP_C_FLAGS} + LINK_FLAGS ${OpenMP_C_FLAGS}) + add_test(test-magma ${BML_NONMPI_PRECOMMAND} ${BML_NONMPI_PRECOMMAND_ARGS} + ${CMAKE_CURRENT_BINARY_DIR}/test-magma) +endif() + set(SOURCES_TYPED add_matrix_typed.c adjacency_matrix_typed.c diff --git a/tests/C-tests/test_magma.c b/tests/C-tests/test_magma.c new file mode 100644 index 00000000..29c09ec7 --- /dev/null +++ b/tests/C-tests/test_magma.c @@ -0,0 +1,81 @@ +#include "magma_v2.h" + +#include +#include +#include +#include + +int +main( + int argc, + char *argv[]) +{ + /* matrix dimensions */ + int na = 100; + + printf("==========================================\n"); + printf("MAGMA simple test\n"); + + printf("Matrix size: %d\n", na); + + /* allocate the matrices needed */ + double *da; + magma_int_t ret = magma_dmalloc(&da, na * na); + if (ret != MAGMA_SUCCESS) + { + printf("MAGMA allocation failed!\n"); + return 1; + } + + printf("Set matrix on CPU...\n"); + double *ha = calloc(na * na, sizeof(double)); + for (int i = 0; i < na; i++) + { + for (int j = 0; j <= i; j++) + { + ha[i * na + j] = rand() / (double) RAND_MAX; + if (i != j) + ha[j * na + i] = ha[i * na + j]; + } + } + + int device; + magma_getdevice(&device); + magma_queue_t queue; + magma_queue_create(device, &queue); + + printf("Set matrix on GPU...\n"); + magma_dsetmatrix(na, na, ha, na, da, na, queue); + + magma_queue_sync(queue); + + /* copy data back to CPU */ + double *hb = calloc(na * na, sizeof(double)); + magma_dgetmatrix(na, na, da, na, hb, na, queue); + + /* check data on CPU */ + int status = 0; + for (int i = 0; i < na * na; i++) + { + if (fabs(ha[i] - hb[i]) > 1.e-6) + { + status = 1; + printf("index %d, ha = %le, hb = %le\n", i, ha[i], hb[i]); + } + } + if (status == 1) + return 1; + + printf("Free resources...\n"); + ret = magma_free(da); + if (ret != MAGMA_SUCCESS) + { + printf("MAGMA free failed!\n"); + return 1; + } + + free(ha); + free(hb); + + return 0; +}