47 int cart_dims[2], cart_periods[2], cart_coords[2];
51 assert(0 < nrows && 0 < ncols);
52 int *row_dist = malloc(nrows *
sizeof(
int));
53 int *col_dist = malloc(ncols *
sizeof(
int));
54 assert(row_dist != NULL && col_dist != NULL);
55 for (
int i = 0;
i < nrows;
i++) {
56 row_dist[
i] =
i % cart_dims[0];
58 for (
int i = 0;
i < ncols;
i++) {
59 col_dist[
i] =
i % cart_dims[1];
63 dbm_distribution_new(&dist, fortran_comm, nrows, ncols, row_dist, col_dist);
75 const int nrows_max,
const int ncols_min,
81 assert(0 < nrows && 0 < ncols);
82 int *row_sizes = malloc(nrows *
sizeof(
int));
83 int *col_sizes = malloc(ncols *
sizeof(
int));
84 assert(row_sizes != NULL && col_sizes != NULL);
85 assert(0 < nrows_min && nrows_min <= nrows_max);
86 assert(0 < ncols_min && ncols_min <= ncols_max);
87 if (nrows_min != nrows_max) {
88 const int row_size = nrows_max - nrows_min + 1;
89 for (
int i = 0;
i < nrows;
i++) {
90 row_sizes[
i] = rand() % row_size + 1;
93 for (
int i = 0;
i < nrows;
i++) {
94 row_sizes[
i] = nrows_max;
97 if (ncols_min != ncols_max) {
98 const int col_size = ncols_max - ncols_min + 1;
99 for (
int i = 0;
i < ncols;
i++) {
100 col_sizes[
i] = rand() % col_size + 1;
103 for (
int i = 0;
i < ncols;
i++) {
104 col_sizes[
i] = ncols_max;
108 dbm_create(&matrix, dist,
"some name", nrows, ncols, row_sizes, col_sizes);
109 dbm_distribution_release(dist);
121 const int *row_sizes, *col_sizes;
128#pragma omp for collapse(2)
129 for (
int row = 0; row < nrows; row++) {
130 for (
int col = 0; col < ncols; col++) {
131 if (dbm_get_stored_coordinates(matrix, row, col) ==
138 int *reserve_row = malloc(nblocks *
sizeof(
int));
139 int *reserve_col = malloc(nblocks *
sizeof(
int));
140 assert(reserve_row != NULL && reserve_col != NULL);
142#pragma omp for collapse(2)
143 for (
int row = 0; row < nrows; row++) {
144 for (
int col = 0; col < ncols; col++) {
145 if (dbm_get_stored_coordinates(matrix, row, col) ==
147 reserve_row[iblock] = row;
148 reserve_col[iblock] = col;
153 assert(iblock == nblocks);
154 dbm_reserve_blocks(matrix, nblocks, reserve_row, reserve_col);
191 dbm_create(&matrix_c, dist_c,
"result", M, N, matrix_a->
row_sizes,
193 dbm_distribution_release(dist_c);
200 const char *
const verify_env = getenv(
"DBM_MULTIPLY_VERIFY");
201 const int skip_verify = (NULL == verify_env ? 0 : (atoi(verify_env) + 1));
203 if (0 == skip_verify) {
205 dbm_create(&matrix_d, dist_shared, matrix_c->
name, matrix_c->
nrows,
207 dbm_copy(matrix_d, matrix_c);
211 const double time_start_multiply = omp_get_wtime();
212 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_c,
false,
214 const double time_end_multiply = omp_get_wtime();
217 printf(
"%5i x %5i x %5i with %3i x %3i x %3i blocks: ", M, N, K, m, n, k);
220 if (NULL != matrix_d) {
221 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_d,
false,
224 const double maxeps = 1E-5, epsilon =
dbm_maxeps(matrix_d, matrix_c);
225 if (maxeps < epsilon) {
227 fprintf(stderr,
"Failed validation (epsilon=%f).\n", epsilon);
230 dbm_release(matrix_d);
235 const double duration = time_end_multiply - time_start_multiply;
236 printf(
"%6.3f s => %6.1f GFLOP/s\n", duration, 1e-9 * flop / duration);
240 dbm_release(matrix_a);
241 dbm_release(matrix_b);
242 dbm_release(matrix_c);
249int main(
int argc,
char *argv[]) {
250 int result = EXIT_SUCCESS;
261 if (offload_get_device_count() > 0) {
262 offload_set_chosen_device(my_rank % offload_get_device_count());
266 int dims[2] = {0, 0};
268 const int periods[2] = {
true,
true};
272 printf(
"OpenMP-threads: %i GPUs: %i", omp_get_max_threads(),
273 imin(offload_get_device_count(), nranks));
274#if defined(__LIBXSMM)
275 printf(
" Libxsmm: %s", LIBXSMM_VERSION);
277 printf(
" Libxsmm: n/a");
279#if defined(__parallel)
280 printf(
" MPI-ranks: %i MPI-cart: %i x %i", nranks, dims[0], dims[1]);
312 FILE *
const file = fopen(argv[1],
"r");
314 const char delims[] =
"x,;:|/\t ";
315 int mnk[] = {0, 0, 0},
i = 1, j = 0;
317 (NULL == file || NULL != fgets(buffer,
sizeof(buffer), file))) {
318 const char *arg = strtok(NULL != file ? buffer : argv[
i], delims);
319 for (; NULL != arg && j < 3; arg = strtok(NULL, delims), ++j) {
324 }
else if (++
i < argc) {
328 const int m = mnk[0];
329 const int n = (0 < mnk[1] ? mnk[1] : m);
330 const int k = (0 < mnk[2] ? mnk[2] : m);
331 int M = (NULL == arg ? 0 : atoi(arg)), N, K;
333 arg = strtok(NULL, delims);
334 N = (NULL == arg ? 1 : atoi(arg));
335 arg = strtok(NULL, delims);
336 K = (NULL == arg ? 1 : atoi(arg));
341 mnk[0] = mnk[1] = mnk[2] = 0;
343 fprintf(stderr,
"ERROR: invalid argument(s)\n");
344 result = EXIT_FAILURE;
353 if (EXIT_SUCCESS == result) {
355 dbm_library_print_stats(fortran_comm, &
print_func, my_rank);
356 offload_mempool_stats_print(fortran_comm, &
print_func, my_rank);
358 dbm_library_finalize();