1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "smpi_mpi_dt_private.h"
12 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
15 "Logging specific to SMPI (kernel)");
17 typedef struct s_smpi_process_data {
19 xbt_fifo_t pending_sent;
20 xbt_fifo_t pending_recv;
23 } s_smpi_process_data_t;
25 static smpi_process_data_t* process_data = NULL;
26 static int process_count = 0;
28 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
30 smpi_process_data_t smpi_process_data(void) {
31 return SIMIX_process_get_data(SIMIX_process_self());
34 smpi_process_data_t smpi_process_remote_data(int index) {
35 return process_data[index];
38 int smpi_process_count(void) {
42 int smpi_process_index(void) {
43 smpi_process_data_t data = smpi_process_data();
48 xbt_os_timer_t smpi_process_timer(void) {
49 smpi_process_data_t data = smpi_process_data();
54 MPI_Comm smpi_process_comm_self(void) {
55 smpi_process_data_t data = smpi_process_data();
57 return data->comm_self;
60 void print_request(const char* message, MPI_Request request) {
61 char* req = bprintf("[buf = %p, size = %zu, src = %d, dst = %d, tag= %d, complete = %d, flags = %u]",
62 request->buf, request->size, request->src, request->dst, request->tag, request->complete, request->flags);
64 DEBUG5("%s (request %p with rdv %p and match %p) %s",
65 message, request, request->rdv, request->match, req);
69 void smpi_process_post_send(MPI_Comm comm, MPI_Request request) {
70 int index = smpi_group_index(smpi_comm_group(comm), request->dst);
71 smpi_process_data_t data = smpi_process_remote_data(index);
75 print_request("Isend", request);
76 xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
77 if(req->comm == request->comm
78 && (req->src == MPI_ANY_SOURCE || req->src == request->src)
79 && (req->tag == MPI_ANY_TAG || req->tag == request->tag)){
80 print_request("Match found", req);
81 xbt_fifo_remove_item(data->pending_recv, item);
82 /* Materialize the *_ANY_* fields from corresponding irecv request */
83 req->src = request->src;
84 req->tag = request->tag;
86 request->rdv = req->rdv;
91 request->rdv = SIMIX_rdv_create(NULL);
92 xbt_fifo_push(data->pending_sent, request);
95 void smpi_process_post_recv(MPI_Request request) {
96 smpi_process_data_t data = smpi_process_data();
100 print_request("Irecv", request);
101 xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
102 if(req->comm == request->comm
103 && (request->src == MPI_ANY_SOURCE || req->src == request->src)
104 && (request->tag == MPI_ANY_TAG || req->tag == request->tag)){
105 print_request("Match found", req);
106 xbt_fifo_remove_item(data->pending_sent, item);
107 /* Materialize the *_ANY_* fields from the irecv request */
108 req->match = request;
109 request->src = req->src;
110 request->tag = req->tag;
111 request->rdv = req->rdv;
112 request->match = req;
116 request->rdv = SIMIX_rdv_create(NULL);
117 xbt_fifo_push(data->pending_recv, request);
120 void smpi_global_init(void) {
124 SIMIX_network_set_copy_data_callback(&SIMIX_network_copy_buffer_callback);
125 process_count = SIMIX_process_count();
126 process_data = xbt_new(smpi_process_data_t, process_count);
127 for(i = 0; i < process_count; i++) {
128 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
129 process_data[i]->index = i;
130 process_data[i]->pending_sent = xbt_fifo_new();
131 process_data[i]->pending_recv = xbt_fifo_new();
132 process_data[i]->timer = xbt_os_timer_new();
133 group = smpi_group_new(1);
134 process_data[i]->comm_self = smpi_comm_new(group);
135 smpi_group_set_mapping(group, i, 0);
137 group = smpi_group_new(process_count);
138 MPI_COMM_WORLD = smpi_comm_new(group);
139 for(i = 0; i < process_count; i++) {
140 smpi_group_set_mapping(group, i, i);
144 void smpi_global_destroy(void) {
145 int count = smpi_process_count();
148 smpi_comm_destroy(MPI_COMM_WORLD);
149 MPI_COMM_WORLD = MPI_COMM_NULL;
150 for(i = 0; i < count; i++) {
151 smpi_comm_destroy(process_data[i]->comm_self);
152 xbt_os_timer_free(process_data[i]->timer);
153 xbt_fifo_free(process_data[i]->pending_recv);
154 xbt_fifo_free(process_data[i]->pending_sent);
155 xbt_free(process_data[i]);
157 xbt_free(process_data);
161 int main(int argc, char **argv)
163 srand(SMPI_RAND_SEED);
165 double default_reference_speed = 20000.0;
166 xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
167 "Power of the host running the simulation (in flop/s). Used to bench the operations.",
168 xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL, NULL);
170 int default_display_timing = 0;
171 xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
172 "Boolean indicating whether we should display the timing after simulation.",
173 xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
175 int default_display_smpe = 0;
176 xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
177 "Boolean indicating whether we should display simulated time spent in MPI calls.",
178 xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL, NULL);
180 double default_threshold = 1e-6;
181 xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
182 "Minimal computation time (in seconds) not discarded.",
183 xbt_cfgelm_double, &default_threshold, 1, 1, NULL, NULL);
185 SIMIX_global_init(&argc, argv);
187 // parse the platform file: get the host list
188 SIMIX_create_environment(argv[1]);
190 SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
191 SIMIX_launch_application(argv[2]);
195 /* Clean IO before the run */
200 while (SIMIX_solve(NULL, NULL) != -1.0);
202 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
203 INFO1("simulation time %g", SIMIX_get_clock());
205 smpi_global_destroy();
207 SIMIX_message_sizes_output("toto.txt");