Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple magma test #741

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}