NewMadeleine

Documentation

nm_sr_custom_data.c
/*
* NewMadeleine
* Copyright (C) 2006-2024 (see AUTHORS file)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
struct user_data_s
{
char**array;
int num;
};
static void user_data_traversal(const void*_content, nm_data_apply_t apply, void*_context) __attribute__((unused));
static const struct nm_data_ops_s user_data_ops =
{
.p_traversal = &user_data_traversal,
.p_generator_init = NULL,
.p_generator_next = NULL
};
NM_DATA_TYPE(user_data, struct user_data_s, &user_data_ops);
static void user_data_traversal(const void*_content, nm_data_apply_t apply, void*_context)
{
const struct user_data_s*content = _content;
int i;
for(i = 0; i < content->num; i++)
{
(*apply)(content->array[i], content->len, _context);
}
}
int main(int argc, char**argv)
{
static const nm_tag_t tag = 0x01;
/* init ring topology */
nm_launcher_init(&argc, argv);
nm_session_open(&p_session, "nm_sr_custom_data");
int rank, size;
nm_gate_t p_gate_next, p_gate_prev;
nm_launcher_get_gate((rank + 1) % size, &p_gate_next);
nm_launcher_get_gate((rank - 1 + size) % size, &p_gate_prev);
/* init array, filled only on rank #0 */
int num = 200;
int len = 100;
char**array = malloc(sizeof(char*) * num);
int i;
for(i = 0; i < num; i++)
{
array[i] = malloc(len);
if(rank == 0)
{
int j;
for(j = 0; j < len - 1; j++)
{
array[i][j] = 'a' + ((i + j) % 26);
}
array[i][len - 1] = '\0';
}
}
/* create data descriptor */
struct nm_data_s data;
nm_data_user_data_set(&data, (struct user_data_s){ .array = array, .num = num, .len = len });
if(rank == 0)
{
/* first link in the ring */
nm_sr_request_t request;
nm_sr_isend_data(p_session, p_gate_next, tag, &data, &request);
nm_sr_swait(p_session, &request);
}
else if(rank == size - 1)
{
/* last link in the ring */
nm_sr_request_t request;
nm_sr_irecv_data(p_session, p_gate_prev, tag, &data, &request);
nm_sr_rwait(p_session, &request);
printf("received data on node %d:\n", rank);
for(i = 0; i < num; i++)
{
printf("%s\n", array[i]);
}
}
else
{
/* forward data */
nm_sr_request_t request;
nm_sr_irecv_data(p_session, p_gate_prev, tag, &data, &request);
nm_sr_rwait(p_session, &request);
nm_sr_isend_data(p_session, p_gate_next, tag, &data, &request);
nm_sr_swait(p_session, &request);
}
for(i = 0; i < num; i++)
{
free(array[i]);
}
free(array);
return 0;
}
struct nm_core_event_s __attribute__
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.
void(* nm_data_apply_t)(void *ptr, nm_len_t len, void *_context)
function to apply to each data chunk upon traversal
Definition: nm_data.h:100
#define NM_DATA_TYPE(ENAME, CONTENT_TYPE, OPS)
macro to generate typed functions to init/access data fields.
Definition: nm_data.h:201
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.
nm_tag_t tag
the user-supplied tag
static nm_session_t p_session
uint16_t len
chunk len
Definition: nm_headers.h:0
nm_len_t size
size of the onsided data (not incuding target-side completion)
static int nm_sr_isend_data(nm_session_t p_session, nm_gate_t p_gate, nm_tag_t tag, struct nm_data_s *p_data, nm_sr_request_t *p_request)
static int nm_sr_swait(nm_session_t p_session, nm_sr_request_t *p_request)
Wait for the completion of a non blocking send request.
static int nm_sr_rwait(nm_session_t p_session, nm_sr_request_t *p_request)
Wait for the completion of a non blocking receive request.
static int nm_sr_irecv_data(nm_session_t p_session, nm_gate_t p_gate, nm_tag_t tag, struct nm_data_s *p_data, nm_sr_request_t *p_request)
uint64_t nm_tag_t
user tags, 64 bits, contained in indirect hashtable
Definition: nm_types.h:58
uint64_t nm_len_t
data length used by nmad
Definition: nm_types.h:70
set of operations available on data type.
Definition: nm_data.h:175
nm_data_traversal_t p_traversal
operation to apply a given function to all chunks of data (required)
Definition: nm_data.h:176
a data descriptor, used to pack/unpack data from app layout to/from contiguous buffers
Definition: nm_data.h:189
Connection to another process.
Definition: nm_gate.h:100
internal defintion of the sendrecv request