Skip to content

Commit

Permalink
Add simple magma test (#741)
Browse files Browse the repository at this point in the history
To be run upfront to make sure magma works for BML
  • Loading branch information
jeanlucf22 authored Oct 16, 2023
1 parent 02056a8 commit 1a84f81
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/C-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions tests/C-tests/test_magma.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "magma_v2.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>

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;
}

0 comments on commit 1a84f81

Please sign in to comment.