NewMadeleine

Documentation

« back to PM2 home.
nm_examples_helper.h
Go to the documentation of this file.
1/*
2 * NewMadeleine
3 * Copyright (C) 2006-2026 (see AUTHORS file)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15
16#include <nm_public.h>
19#include <nm_coll_interface.h>
20
21
22/* ********************************************************* */
23
24static int is_server = -1;
25static nm_session_t p_session = NULL;
26static nm_gate_t p_gate = NULL;
27static nm_comm_t p_comm = NULL;
28
29static const nm_tag_t data_tag = 0x01;
30static const nm_tag_t sync_tag = 0x02;
31
38
39static void nm_examples_init_topo(int*argc, char*argv[], enum nm_example_topo_e topo)
40{
41 int rank, size, peer;
42 nm_launcher_init(argc, argv);
43 nm_session_open(&p_session, "nm_example");
46 switch(topo)
47 {
49 is_server = (rank == 0);
50 peer = (rank + 1) % size;
51 break;
53 if(size % 2 != 0)
54 {
55 fprintf(stderr, "%s: FATAL- needs an even number of nodes.\n", argv[0]);
56 abort();
57 }
58 is_server = ((rank % 2) == 0);
59 peer = is_server ? (rank + 1) : (rank - 1);
60 break;
62 is_server = (rank == 0);
63 peer = 0;
64 break;
65 default:
66 abort();
67 break;
68 }
70 p_comm = nm_comm_world("nm_example");
71}
72
73/* barrier accross all nodes */
74static inline void nm_examples_barrier(nm_tag_t tag)
75{
77}
78
79static inline void nm_examples_init(int*argc, char*argv[])
80{
82}
83
84static inline void nm_examples_exit(void)
85{
86 nm_examples_barrier(0xFFFF);
87 if(p_comm)
91}
92
93
94/* ********************************************************* */
95
96/* ** helpers for size iterators */
97
98static inline nm_len_t _next(nm_len_t len, double multiplier, nm_len_t increment)
99{
100 nm_len_t next = len * multiplier + increment;
101 if(next <= len)
102 next++;
103 return next;
104}
105
106/* ** helpers for buffers */
107
108static inline char buffer_content(nm_len_t offset, int seed)
109{
110 return 'a' + ((seed + offset) % 26);
111}
112
113static inline void fill_buffer_seed(char*buffer, nm_len_t len, int seed) __attribute__((unused));
114static inline void fill_buffer_seed(char*buffer, nm_len_t len, int seed)
115{
116 nm_len_t i = 0;
117 for(i = 0; i < len; i++)
118 {
119 buffer[i] = buffer_content(i, seed);
120 }
121}
122
123static inline void fill_buffer(char*buffer, nm_len_t len) __attribute__((unused));
124static inline void fill_buffer(char*buffer, nm_len_t len)
125{
126 fill_buffer_seed(buffer, len , 0);
127}
128
129static inline void clear_buffer(char*buffer, nm_len_t len) __attribute__((unused));
130static inline void clear_buffer(char*buffer, nm_len_t len)
131{
132 memset(buffer, 0, len);
133}
134
135static inline void control_buffer_seed(const char*buffer, nm_len_t len, int seed) __attribute__((unused));
136static inline void control_buffer_seed(const char*buffer, nm_len_t len, int seed)
137{
138 nm_len_t i = 0;
139 for(i = 0; i < len; i++)
140 {
141 const char expected = buffer_content(i, seed);
142 if(buffer[i] != expected)
143 {
144 fprintf(stderr, "Bad data at byte %d: expected 0x%x (%c), received 0x%x (%c)\n",
145 (int)i, expected, expected, buffer[i], buffer[i]);
146 abort();
147 }
148 }
149}
150
151static inline void control_buffer(const char*buffer, nm_len_t len) __attribute__((unused));
152static inline void control_buffer(const char*buffer, nm_len_t len)
153{
154 control_buffer_seed(buffer, len, 0);
155}
156
157static inline void compute(unsigned usec)
158{
159 if(usec > 0)
160 {
161 puk_tick_t t1, t2;
162 PUK_GET_TICK(t1);
163 do
164 {
165 PUK_GET_TICK(t2);
166 }
167 while(PUK_TIMING_DELAY(t1,t2) < usec);
168 }
169}
170
171#define check_rc(RC) \
172 if((RC) != NM_ESUCCESS) \
173 { \
174 fprintf(stderr, "Error %d (%s) in %s line %d\n", \
175 (RC), nm_strerror(RC), __FILE__, __LINE__); \
176 abort(); \
177 }
178
180static inline void fisher_yates_shuffle(int*p_array, size_t n)
181{
182 srand(n); /* ensure all nodes use the same seed */
183 size_t i;
184 for(i = 0; i < n - 1; i++)
185 {
186 const int r = rand() % (n - 1 - i);
187 const int tmp = p_array[i];
188 p_array[i] = p_array[i + r];
189 p_array[i + r] = tmp;
190 }
191}
192
194static inline struct nm_group_s*nm_examples_group_inorder(int n)
195{
196 nm_group_t p_group = nm_group_new();
197 int i;
198 for(i = 0; i < n; i++)
199 {
201 }
202 return p_group;
203}
204
206static inline struct nm_group_s*nm_examples_group_reverse(int n)
207{
208 nm_group_t p_group = nm_group_new();
209 int i;
210 for(i = n - 1; i >= 0; i--)
211 {
213 }
214 return p_group;
215}
216
217static inline struct nm_group_s*nm_examples_group_shuffled(int n)
218{
219 int*p_index = padico_malloc(sizeof(int) * n);
220 int i;
221 for(i = 0; i < n; i++)
222 {
223 p_index[i] = i;
224 }
225 fisher_yates_shuffle(p_index, n);
226 nm_group_t p_group = nm_group_new();
227 for(i = 0; i < n; i++)
228 {
229 nm_group_add_node(p_group, nm_comm_get_gate(p_comm, p_index[i]));
230 }
231 padico_free(p_index);
232 return p_group;
233}
void nm_coll_barrier(nm_comm_t comm, nm_tag_t tag)
void nm_comm_destroy(nm_comm_t p_comm)
destroy a communicator- no synchronization is done
nm_comm_t nm_comm_world(const char *label)
create a new global communicator with given label
nm_group_t nm_group_new(void)
create a new empty group
void nm_group_add_node(nm_group_t p_group, nm_gate_t p_gate)
struct nm_group_s * nm_group_t
type for groups
Definition nm_group.h:38
int nm_launcher_get_rank(int *rank)
Returns process rank.
int nm_launcher_get_size(int *size)
Returns the number of nodes.
int nm_launcher_exit(void)
Cleans session.
int nm_launcher_get_gate(int dest, nm_gate_t *gate)
Returns the gate for the process dest.
static int nm_launcher_init(int *argc, char **argv)
Initializes nmad.
struct nm_core_event_s __attribute__
Definition nm_data.h:538
int nm_session_open(nm_session_t *pp_session, const char *label)
Open a new session, assuming processes are already connected.
int nm_session_close(nm_session_t p_session)
Disconnect and destroy a session.
static nm_gate_t nm_comm_get_gate(nm_comm_t p_comm, int rank)
nm_tag_t tag
the user-supplied tag
static nm_comm_t p_comm
static int is_server
static struct nm_group_s * nm_examples_group_shuffled(int n)
nm_example_topo_e
@ NM_EXAMPLES_TOPO_STAR
@ NM_EXAMPLES_TOPO_PAIRS
@ NM_EXAMPLES_TOPO_RING
static void control_buffer(const char *buffer, nm_len_t len) __attribute__((unused))
static struct nm_group_s * nm_examples_group_inorder(int n)
generate a group with ranks in order
static void fill_buffer(char *buffer, nm_len_t len) __attribute__((unused))
static struct nm_group_s * nm_examples_group_reverse(int n)
generate a group with ranks in reverse order
static void nm_examples_barrier(nm_tag_t tag)
static void clear_buffer(char *buffer, nm_len_t len) __attribute__((unused))
static void nm_examples_init(int *argc, char *argv[])
static char buffer_content(nm_len_t offset, int seed)
static void fill_buffer_seed(char *buffer, nm_len_t len, int seed) __attribute__((unused))
static const nm_tag_t sync_tag
static nm_session_t p_session
static void nm_examples_exit(void)
static void fisher_yates_shuffle(int *p_array, size_t n)
shuffle an array of ints
static void nm_examples_init_topo(int *argc, char *argv[], enum nm_example_topo_e topo)
static nm_gate_t p_gate
static nm_len_t _next(nm_len_t len, double multiplier, nm_len_t increment)
static void compute(unsigned usec)
static void control_buffer_seed(const char *buffer, nm_len_t len, int seed) __attribute__((unused))
static const nm_tag_t data_tag
uint16_t len
chunk len
Definition nm_headers.h:0
nm_len_t size
size of the onsided data (not incuding target-side completion)
This is the common public header for NewMad.
uint64_t nm_tag_t
user tags, 64 bits, contained in indirect hashtable
Definition nm_types.h:56
uint64_t nm_len_t
data length used by nmad
Definition nm_types.h:68
Connection to another process.
Definition nm_gate.h:104