46 int cart_dims[2], cart_periods[2], cart_coords[2];
50 assert(0 < nrows && 0 < ncols);
51 int *row_dist = malloc(nrows *
sizeof(
int));
52 int *col_dist = malloc(ncols *
sizeof(
int));
53 assert(row_dist != NULL && col_dist != NULL);
54 for (
int i = 0;
i < nrows;
i++) {
55 row_dist[
i] =
i % cart_dims[0];
57 for (
int i = 0;
i < ncols;
i++) {
58 col_dist[
i] =
i % cart_dims[1];
62 dbm_distribution_new(&dist, fortran_comm, nrows, ncols, row_dist, col_dist);
74 const int nrows_max,
const int ncols_min,
80 assert(0 < nrows && 0 < ncols);
81 int *row_sizes = malloc(nrows *
sizeof(
int));
82 int *col_sizes = malloc(ncols *
sizeof(
int));
83 assert(row_sizes != NULL && col_sizes != NULL);
84 assert(0 < nrows_min && nrows_min <= nrows_max);
85 assert(0 < ncols_min && ncols_min <= ncols_max);
86 if (nrows_min != nrows_max) {
87 const int row_size = nrows_max - nrows_min + 1;
88 for (
int i = 0;
i < nrows;
i++) {
89 row_sizes[
i] = rand() % row_size + 1;
92 for (
int i = 0;
i < nrows;
i++) {
93 row_sizes[
i] = nrows_max;
96 if (ncols_min != ncols_max) {
97 const int col_size = ncols_max - ncols_min + 1;
98 for (
int i = 0;
i < ncols;
i++) {
99 col_sizes[
i] = rand() % col_size + 1;
102 for (
int i = 0;
i < ncols;
i++) {
103 col_sizes[
i] = ncols_max;
107 dbm_create(&matrix, dist,
"some name", nrows, ncols, row_sizes, col_sizes);
108 dbm_distribution_release(dist);
120 const int *row_sizes, *col_sizes;
127#pragma omp for collapse(2)
128 for (
int row = 0; row < nrows; row++) {
129 for (
int col = 0; col < ncols; col++) {
130 if (dbm_get_stored_coordinates(matrix, row, col) ==
137 int *reserve_row = malloc(nblocks *
sizeof(
int));
138 int *reserve_col = malloc(nblocks *
sizeof(
int));
139 assert(reserve_row != NULL && reserve_col != NULL);
141#pragma omp for collapse(2)
142 for (
int row = 0; row < nrows; row++) {
143 for (
int col = 0; col < ncols; col++) {
144 if (dbm_get_stored_coordinates(matrix, row, col) ==
146 reserve_row[iblock] = row;
147 reserve_col[iblock] = col;
152 assert(iblock == nblocks);
153 dbm_reserve_blocks(matrix, nblocks, reserve_row, reserve_col);
190 dbm_create(&matrix_c, dist_c,
"result", M, N, matrix_a->
row_sizes,
192 dbm_distribution_release(dist_c);
200 dbm_create(&matrix_d, dist_shared, matrix_c->
name, matrix_c->
nrows,
202 dbm_copy(matrix_d, matrix_c);
205 const double time_start_multiply = omp_get_wtime();
206 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_c,
false,
208 const double time_end_multiply = omp_get_wtime();
211 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_d,
false,
215 printf(
"%5i x %5i x %5i with %3i x %3i x %3i blocks: ", M, N, K, m, n, k);
219 const double maxeps = 1E-5, epsilon =
dbm_maxeps(matrix_d, matrix_c);
220 if (maxeps >= epsilon) {
223 const double duration = time_end_multiply - time_start_multiply;
224 printf(
"%6.3f s => %6.1f GFLOP/s\n", duration, 1e-9 * flop / duration);
229 fprintf(stderr,
"Failed validation (epsilon=%f).\n", epsilon);
233 dbm_release(matrix_a);
234 dbm_release(matrix_b);
235 dbm_release(matrix_c);
236 dbm_release(matrix_d);
243int main(
int argc,
char *argv[]) {
244 int result = EXIT_SUCCESS;
255 if (offload_get_device_count() > 0) {
256 offload_set_chosen_device(my_rank % offload_get_device_count());
260 int dims[2] = {0, 0};
262 const int periods[2] = {
true,
true};
267 printf(
"OpenMP-threads: %i GPUs: %i", omp_get_max_threads(),
268 imin(offload_get_device_count(), nranks));
269#if defined(__LIBXSMM)
270 printf(
" Libxsmm: %s", LIBXSMM_VERSION);
272 printf(
" Libxsmm: n/a");
274#if defined(__parallel)
275 printf(
" MPI-ranks: %i MPI-cart: %i x %i", nranks, dims[0], dims[1]);
307 FILE *
const file = fopen(argv[1],
"r");
309 const char delims[] =
"x,;:|/\t ";
310 int mnk[] = {0, 0, 0},
i = 1, j = 0;
312 (NULL == file || NULL != fgets(buffer,
sizeof(buffer), file))) {
313 const char *arg = strtok(NULL != file ? buffer : argv[
i], delims);
314 for (; NULL != arg && j < 3; arg = strtok(NULL, delims), ++j) {
319 }
else if (++
i < argc) {
323 const int m = mnk[0];
324 const int n = (0 < mnk[1] ? mnk[1] : m);
325 const int k = (0 < mnk[2] ? mnk[2] : m);
326 int M = (NULL == arg ? 0 : atoi(arg)), N, K;
328 arg = strtok(NULL, delims);
329 N = (NULL == arg ? 1 : atoi(arg));
330 arg = strtok(NULL, delims);
331 K = (NULL == arg ? 1 : atoi(arg));
336 mnk[0] = mnk[1] = mnk[2] = 0;
338 fprintf(stderr,
"ERROR: invalid argument(s)\n");
339 result = EXIT_FAILURE;
348 if (EXIT_SUCCESS == result) {
351 dbm_library_finalize();