Skip to content

Commit 1a84f81

Browse files
authored
Add simple magma test (lanl#741)
To be run upfront to make sure magma works for BML
1 parent 02056a8 commit 1a84f81

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

tests/C-tests/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ if(BML_OMP_OFFLOAD)
1111
${CMAKE_CURRENT_BINARY_DIR}/test-openmp_offload)
1212
endif()
1313

14+
if(BML_MAGMA)
15+
add_executable(test-magma test_magma.c)
16+
target_link_libraries(test-magma ${LINK_LIBRARIES})
17+
set_target_properties(test-magma
18+
PROPERTIES
19+
COMPILE_FLAGS ${OpenMP_C_FLAGS}
20+
LINK_FLAGS ${OpenMP_C_FLAGS})
21+
add_test(test-magma ${BML_NONMPI_PRECOMMAND} ${BML_NONMPI_PRECOMMAND_ARGS}
22+
${CMAKE_CURRENT_BINARY_DIR}/test-magma)
23+
endif()
24+
1425
set(SOURCES_TYPED
1526
add_matrix_typed.c
1627
adjacency_matrix_typed.c

tests/C-tests/test_magma.c

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "magma_v2.h"
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <assert.h>
6+
#include <sys/time.h>
7+
8+
int
9+
main(
10+
int argc,
11+
char *argv[])
12+
{
13+
/* matrix dimensions */
14+
int na = 100;
15+
16+
printf("==========================================\n");
17+
printf("MAGMA simple test\n");
18+
19+
printf("Matrix size: %d\n", na);
20+
21+
/* allocate the matrices needed */
22+
double *da;
23+
magma_int_t ret = magma_dmalloc(&da, na * na);
24+
if (ret != MAGMA_SUCCESS)
25+
{
26+
printf("MAGMA allocation failed!\n");
27+
return 1;
28+
}
29+
30+
printf("Set matrix on CPU...\n");
31+
double *ha = calloc(na * na, sizeof(double));
32+
for (int i = 0; i < na; i++)
33+
{
34+
for (int j = 0; j <= i; j++)
35+
{
36+
ha[i * na + j] = rand() / (double) RAND_MAX;
37+
if (i != j)
38+
ha[j * na + i] = ha[i * na + j];
39+
}
40+
}
41+
42+
int device;
43+
magma_getdevice(&device);
44+
magma_queue_t queue;
45+
magma_queue_create(device, &queue);
46+
47+
printf("Set matrix on GPU...\n");
48+
magma_dsetmatrix(na, na, ha, na, da, na, queue);
49+
50+
magma_queue_sync(queue);
51+
52+
/* copy data back to CPU */
53+
double *hb = calloc(na * na, sizeof(double));
54+
magma_dgetmatrix(na, na, da, na, hb, na, queue);
55+
56+
/* check data on CPU */
57+
int status = 0;
58+
for (int i = 0; i < na * na; i++)
59+
{
60+
if (fabs(ha[i] - hb[i]) > 1.e-6)
61+
{
62+
status = 1;
63+
printf("index %d, ha = %le, hb = %le\n", i, ha[i], hb[i]);
64+
}
65+
}
66+
if (status == 1)
67+
return 1;
68+
69+
printf("Free resources...\n");
70+
ret = magma_free(da);
71+
if (ret != MAGMA_SUCCESS)
72+
{
73+
printf("MAGMA free failed!\n");
74+
return 1;
75+
}
76+
77+
free(ha);
78+
free(hb);
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)