YetAnotherCoupler 3.2.0
Loading...
Searching...
No Matches
interpolation_utils.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <string.h>
6
7#include "utils_core.h"
8#include "interpolation.h"
10#include "yac_mpi_internal.h"
11
13 Xt_redist redist, MPI_Datatype(*xt_redist_get_MPI_Datatype)(Xt_redist, int)) {
14
15 if (redist == NULL) return 0;
16
17 MPI_Comm comm = xt_redist_get_MPI_Comm(redist);
18 int comm_size;
19 yac_mpi_call(MPI_Comm_size(comm, &comm_size), comm);
20
21 size_t max_size = 0;
22
23 for (int i = 0; i < comm_size; ++i) {
24
25 MPI_Datatype dt = xt_redist_get_MPI_Datatype(redist, i);
26 if (dt == MPI_DATATYPE_NULL) continue;
27 MPI_Aint lb, extent;
28 yac_mpi_call(MPI_Type_get_extent(dt, &lb, &extent), comm);
29 size_t curr_size = (size_t)extent + (size_t)lb;
30 if (curr_size > max_size) max_size = curr_size;
31 yac_mpi_call(MPI_Type_free(&dt), comm);
32 }
33 return max_size;
34}
35
36static size_t xt_redist_get_send_buffer_size(Xt_redist redist) {
37
38 return xt_redist_get_buffer_size(redist, xt_redist_get_send_MPI_Datatype);
39}
40
41static size_t xt_redist_get_recv_buffer_size(Xt_redist redist) {
42
43 return xt_redist_get_buffer_size(redist, xt_redist_get_recv_MPI_Datatype);
44}
45
46static size_t * get_buffer_sizes(
47 Xt_redist * redists, size_t num_fields,
49
51 (type == SEND_BUFFER) || (type == RECV_BUFFER),
52 "ERROR(get_buffer_sizes): invalid buffer type");
53
54 size_t (*xt_redist_get_buffer_size)(Xt_redist) =
55 (type == SEND_BUFFER)?
57
58 size_t * buffer_sizes;
59 if (redists == NULL) {
60 buffer_sizes = xcalloc(num_fields, sizeof(*buffer_sizes));
61 } else {
62 buffer_sizes = xmalloc(num_fields * sizeof(*buffer_sizes));
63 for (size_t i = 0; i < num_fields; ++i)
64 buffer_sizes[i] = xt_redist_get_buffer_size(redists[i]);
65 }
66 return buffer_sizes;
67}
68
69static double ** allocate_buffer(
70 size_t * buffer_sizes, size_t num_fields, size_t collection_size) {
71
72 size_t total_buffer_size = 0;
73 for (size_t i = 0; i < num_fields; ++i)
74 total_buffer_size += buffer_sizes[i];
75 double ** buffer_data =
76 xmalloc(collection_size * num_fields * sizeof(*buffer_data));
77 buffer_data[0] = xmalloc(collection_size * total_buffer_size);
78 for (size_t i = 0, offset = 0; i < collection_size; ++i) {
79 for (size_t j = 0; j < num_fields; offset += buffer_sizes[j++]) {
80 buffer_data[i * num_fields + j] =
81 (double*)((char*)(buffer_data[0]) + offset);
82 }
83 }
84 return buffer_data;
85}
86
88 Xt_redist * redists, size_t num_fields, size_t collection_size,
90
91 size_t * buffer_sizes = get_buffer_sizes(redists, num_fields, type);
92
93 return
95 .buffer_sizes = buffer_sizes,
96 .buffer = allocate_buffer(buffer_sizes, num_fields, collection_size)};
97}
98
100 Xt_redist * redists, size_t * min_buffer_sizes, size_t num_fields,
101 size_t collection_size, enum yac_interpolation_buffer_type type) {
102
103
104 size_t * buffer_sizes = get_buffer_sizes(redists, num_fields, type);
105 for (size_t i = 0; i < num_fields; ++i)
106 if (min_buffer_sizes[i] > buffer_sizes[i])
107 buffer_sizes[i] = min_buffer_sizes[i];
108
109 return
110 (struct yac_interpolation_buffer) {
111 .buffer_sizes = buffer_sizes,
112 .buffer = allocate_buffer(buffer_sizes, num_fields, collection_size)};
113}
114
116 struct yac_interpolation_buffer src, size_t num_fields,
117 size_t collection_size) {
118
119 return
120 (struct yac_interpolation_buffer) {
121 .buffer_sizes = COPY_DATA(src.buffer_sizes, num_fields),
122 .buffer =
123 allocate_buffer(src.buffer_sizes, num_fields, collection_size)};
124}
125
127
128 free(buffer->buffer[0]);
129 free(buffer->buffer);
130 free(buffer->buffer_sizes);
131}
enum callback_type type
static double ** allocate_buffer(size_t *buffer_sizes, size_t num_fields, size_t collection_size)
static size_t xt_redist_get_buffer_size(Xt_redist redist, MPI_Datatype(*xt_redist_get_MPI_Datatype)(Xt_redist, int))
struct yac_interpolation_buffer yac_interpolation_buffer_init_2(Xt_redist *redists, size_t *min_buffer_sizes, size_t num_fields, size_t collection_size, enum yac_interpolation_buffer_type type)
static size_t xt_redist_get_recv_buffer_size(Xt_redist redist)
struct yac_interpolation_buffer yac_interpolation_buffer_copy(struct yac_interpolation_buffer src, size_t num_fields, size_t collection_size)
void yac_interpolation_buffer_free(struct yac_interpolation_buffer *buffer)
static size_t * get_buffer_sizes(Xt_redist *redists, size_t num_fields, enum yac_interpolation_buffer_type type)
static size_t xt_redist_get_send_buffer_size(Xt_redist redist)
struct yac_interpolation_buffer yac_interpolation_buffer_init(Xt_redist *redists, size_t num_fields, size_t collection_size, enum yac_interpolation_buffer_type type)
yac_interpolation_buffer_type
@ SEND_BUFFER
@ RECV_BUFFER
Definition __init__.py:1
#define xcalloc(nmemb, size)
Definition ppm_xfuncs.h:64
#define xmalloc(size)
Definition ppm_xfuncs.h:66
#define COPY_DATA(data, count)
Definition utils_core.h:209
#define YAC_ASSERT(exp, msg)
Definition yac_assert.h:16
#define yac_mpi_call(call, comm)