44 int cart_dims[2], cart_periods[2], cart_coords[2];
48 assert(0 < nrows && 0 < ncols);
49 int *row_dist = malloc(nrows *
sizeof(
int));
50 int *col_dist = malloc(ncols *
sizeof(
int));
51 assert(row_dist != NULL && col_dist != NULL);
52 for (
int i = 0;
i < nrows;
i++) {
53 row_dist[
i] =
i % cart_dims[0];
55 for (
int i = 0;
i < ncols;
i++) {
56 col_dist[
i] =
i % cart_dims[1];
60 dbm_distribution_new(&dist, fortran_comm, nrows, ncols, row_dist, col_dist);
72 const int nrows_max,
const int ncols_min,
78 assert(0 < nrows && 0 < ncols);
79 int *row_sizes = malloc(nrows *
sizeof(
int));
80 int *col_sizes = malloc(ncols *
sizeof(
int));
81 assert(row_sizes != NULL && col_sizes != NULL);
82 assert(0 < nrows_min && nrows_min <= nrows_max);
83 assert(0 < ncols_min && ncols_min <= ncols_max);
84 if (nrows_min != nrows_max) {
85 const int row_size = nrows_max - nrows_min + 1;
86 for (
int i = 0;
i < nrows;
i++) {
87 row_sizes[
i] = rand() % row_size + 1;
90 for (
int i = 0;
i < nrows;
i++) {
91 row_sizes[
i] = nrows_max;
94 if (ncols_min != ncols_max) {
95 const int col_size = ncols_max - ncols_min + 1;
96 for (
int i = 0;
i < ncols;
i++) {
97 col_sizes[
i] = rand() % col_size + 1;
100 for (
int i = 0;
i < ncols;
i++) {
101 col_sizes[
i] = ncols_max;
105 dbm_create(&matrix, dist,
"some name", nrows, ncols, row_sizes, col_sizes);
106 dbm_distribution_release(dist);
118 const int *row_sizes, *col_sizes;
125#pragma omp for collapse(2)
126 for (
int row = 0; row < nrows; row++) {
127 for (
int col = 0; col < ncols; col++) {
128 if (dbm_get_stored_coordinates(matrix, row, col) ==
134 int *reserve_row = NULL, *reserve_col = NULL;
136 reserve_row = malloc(nblocks *
sizeof(
int));
137 reserve_col = malloc(nblocks *
sizeof(
int));
138 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);
199 const char *
const verify_env = getenv(
"DBM_MULTIPLY_VERIFY");
200 const int skip_verify = (NULL == verify_env ? 0 : (atoi(verify_env) + 1));
202 if (0 == skip_verify) {
204 dbm_create(&matrix_d, dist_shared, matrix_c->
name, matrix_c->
nrows,
206 dbm_copy(matrix_d, matrix_c);
210 const double time_start_multiply = omp_get_wtime();
211 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_c,
false,
213 const double time_end_multiply = omp_get_wtime();
216 printf(
"%5i x %5i x %5i with %3i x %3i x %3i blocks: ", M, N, K, m, n, k);
219 if (NULL != matrix_d) {
220 dbm_multiply(
false,
false, 1.0, matrix_a, matrix_b, 1.0, matrix_d,
false,
223 const double maxeps = 1E-5, epsilon =
dbm_maxeps(matrix_d, matrix_c);
224 if (maxeps < epsilon) {
226 fprintf(stderr,
"Failed validation (epsilon=%f).\n", epsilon);
229 dbm_release(matrix_d);
234 const double duration = time_end_multiply - time_start_multiply;
235 printf(
"%6.3f s => %6.1f GFLOP/s\n", duration, 1e-9 * flop / duration);
239 dbm_release(matrix_a);
240 dbm_release(matrix_b);
241 dbm_release(matrix_c);
248int main(
int argc,
char *argv[]) {
249 int result = EXIT_SUCCESS;
260 if (offload_get_device_count() > 0) {
261 offload_set_chosen_device(my_rank % offload_get_device_count());
265 int dims[2] = {0, 0};
267 const int periods[2] = {
true,
true};
271 printf(
"OpenMP-threads: %i GPUs: %i", omp_get_max_threads(),
272 imin(offload_get_device_count(), nranks));
273#if defined(__parallel)
274 printf(
" MPI-ranks: %i MPI-cart: %i x %i", nranks, dims[0], dims[1]);
306 FILE *
const file = fopen(argv[1],
"r");
308 const char delims[] =
"x,;:|/\t ";
309 int mnk[] = {0, 0, 0},
i = 1, j = 0;
311 (NULL == file || NULL != fgets(buffer,
sizeof(buffer), file))) {
312 const char *arg = strtok(NULL != file ? buffer : argv[
i], delims);
313 for (; NULL != arg && j < 3; arg = strtok(NULL, delims), ++j) {
318 }
else if (++
i < argc) {
322 const int m = mnk[0];
323 const int n = (0 < mnk[1] ? mnk[1] : m);
324 const int k = (0 < mnk[2] ? mnk[2] : m);
325 int M = (NULL == arg ? 0 : atoi(arg)), N, K;
327 arg = strtok(NULL, delims);
328 N = (NULL == arg ? 1 : atoi(arg));
329 arg = strtok(NULL, delims);
330 K = (NULL == arg ? 1 : atoi(arg));
335 mnk[0] = mnk[1] = mnk[2] = 0;
337 fprintf(stderr,
"ERROR: invalid argument(s)\n");
338 result = EXIT_FAILURE;
347 if (EXIT_SUCCESS == result) {
349 dbm_library_print_stats(fortran_comm, &
print_func, my_rank);
350 offload_mempool_stats_print(fortran_comm, &
print_func, my_rank);
352 dbm_library_finalize();