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