25static void print_func(
const char *msg,
int msglen,
int output_unit) {
27 if (output_unit == 0) {
36static inline int imin(
int x,
int y) {
return (x < y ? x : y); }
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];
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);
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++) {
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++) {
146 reserve_row[iblock] = row;
147 reserve_col[iblock] = col;
152 assert(iblock == nblocks);
169 int row, col, row_size, col_size;
172 const int block_size = row_size * col_size;
173 for (
int i = 0;
i < block_size;
i++) {
174 block[
i] = 1.0 / (
i + 1);
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) {
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);
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);
248int main(
int argc,
char *argv[]) {
249 int result = EXIT_SUCCESS;
265 int dims[2] = {0, 0};
267 const int periods[2] = {
true,
true};
271 printf(
"OpenMP-threads: %i GPUs: %i", omp_get_max_threads(),
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) {
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims, const int dims[], const int periods[], const int reorder)
Wrapper around MPI_Cart_create.
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[], int periods[], int coords[])
Wrapper around MPI_Cart_get.
void cp_mpi_sum_int64(int64_t *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
void cp_mpi_comm_free(cp_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
void dbm_distribution_new(dbm_distribution_t **dist_out, const int fortran_comm, const int nrows, const int ncols, const int row_dist[nrows], const int col_dist[ncols])
Creates a new two dimensional distribution.
void dbm_distribution_release(dbm_distribution_t *dist)
Decreases the reference counter of the given distribution.
void dbm_library_finalize(void)
Finalizes the DBM library.
void dbm_library_print_stats(const int fortran_comm, void(*print_func)(const char *, int, int), const int output_unit)
Prints statistics gathered by the DBM library.
void dbm_library_init(void)
Initializes the DBM library.
void dbm_reserve_blocks(dbm_matrix_t *matrix, const int nblocks, const int rows[], const int cols[])
Adds list of blocks efficiently. The blocks will be filled with zeros. This routine must always be ca...
bool dbm_iterator_blocks_left(const dbm_iterator_t *iter)
Tests whether the given iterator has any block left.
void dbm_iterator_stop(dbm_iterator_t *iter)
Releases the given iterator.
void dbm_get_col_sizes(const dbm_matrix_t *matrix, int *ncols, const int **col_sizes)
Returns the column block sizes of the given matrix.
void dbm_create(dbm_matrix_t **matrix_out, dbm_distribution_t *dist, const char name[], const int nrows, const int ncols, const int row_sizes[nrows], const int col_sizes[ncols])
Creates a new matrix.
void dbm_iterator_start(dbm_iterator_t **iter_out, const dbm_matrix_t *matrix)
Creates an iterator for the blocks of the given matrix. The iteration order is not stable....
void dbm_iterator_next_block(dbm_iterator_t *iter, int *row, int *col, double **block, int *row_size, int *col_size)
Returns the next block from the given iterator.
void dbm_get_row_sizes(const dbm_matrix_t *matrix, int *nrows, const int **row_sizes)
Returns the row block sizes of the given matrix.
void dbm_copy(dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b)
Copies content of matrix_b into matrix_a. Matrices must have the same row/col block sizes and distrib...
double dbm_maxeps(const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b)
Calculates maximum relative difference between matrix_a and matrix_b.
int dbm_get_stored_coordinates(const dbm_matrix_t *matrix, const int row, const int col)
Returns the MPI rank on which the given block should be stored.
void dbm_release(dbm_matrix_t *matrix)
Releases a matrix and all its ressources.
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
static dbm_distribution_t * create_dist(const int nrows, const int ncols, const cp_mpi_comm_t comm)
Private routine for creating a distribution.
static int imin(int x, int y)
Returns the smaller of the two integers (missing from the C standard).
void benchmark_multiply(const int M, const int N, const int K, const int m, const int n, const int k, const cp_mpi_comm_t comm)
Run a benchmark of dbm_multiply with given block sizes.
static void set_all_blocks(dbm_matrix_t *matrix)
Private routine for setting all blocks.
static dbm_matrix_t * create_some_matrix(const int nrows, const int ncols, const int nrows_min, const int nrows_max, const int ncols_min, const int ncols_max, const cp_mpi_comm_t comm)
Private routine for creating a distribution and an empty matrix.
static void reserve_all_blocks(dbm_matrix_t *matrix)
Private routine for reserving all blocks of the given matrix.
void dbm_multiply(const bool transa, const bool transb, const double alpha, const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b, const double beta, dbm_matrix_t *matrix_c, const bool retain_sparsity, const double filter_eps, int64_t *flop)
Performs a multiplication of two dbm_matrix_t matrices. See dbm_matrix.h for details.
static void const int const int i
int main()
Unit test of the C-interface provided via libcp2k.h.
int offload_get_device_count(void)
Returns the number of available devices.
void offload_set_chosen_device(int device_id)
Selects the chosen device to be used.
void offload_mempool_stats_print(int fortran_comm, void(*print_func)(const char *, int, int), int output_unit)
Print allocation statistics..
Internal struct for storing a two dimensional distribution.
Internal struct for storing a block iterator.
Internal struct for storing a matrix.
dbm_distribution_t * dist