Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix comm_determinism to work in split MCer/MCed
[simgrid.git] / src / mc / mc_comm_pattern.c
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <string.h>
8
9 #include <xbt/sysdep.h>
10 #include <xbt/dynar.h>
11
12 #include "mc_comm_pattern.h"
13 #include "mc_smx.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_comm_pattern, mc,
16                                 "Logging specific to MC communication patterns");
17
18 mc_comm_pattern_t MC_comm_pattern_dup(mc_comm_pattern_t comm)
19 {
20   mc_comm_pattern_t res = xbt_new0(s_mc_comm_pattern_t, 1);
21   res->index = comm->index;
22   res->type = comm->type;
23   res->comm_addr = comm->comm_addr;
24   res->rdv = strdup(comm->rdv);
25   res->data_size = -1;
26   res->data = NULL;
27   if (comm->type == SIMIX_COMM_SEND) {
28     res->src_proc = comm->src_proc;
29     res->src_host = comm->src_host;
30     if (comm->data != NULL) {
31       res->data_size = comm->data_size;
32       res->data = xbt_malloc0(comm->data_size);
33       memcpy(res->data, comm->data, comm->data_size);
34     }
35   } else {
36     res->dst_proc = comm->dst_proc;
37     res->dst_host = comm->dst_host;
38   }
39   return res;
40 }
41
42 xbt_dynar_t MC_comm_patterns_dup(xbt_dynar_t patterns)
43 {
44   xbt_dynar_t res = xbt_dynar_new(sizeof(mc_comm_pattern_t), MC_comm_pattern_free_voidp);
45
46   mc_comm_pattern_t comm;
47   unsigned int cursor;
48   xbt_dynar_foreach(patterns, cursor, comm) {
49     mc_comm_pattern_t copy_comm = MC_comm_pattern_dup(comm);
50     xbt_dynar_push(res, &copy_comm);
51   }
52
53   return res;
54 }
55
56 static void MC_patterns_copy(xbt_dynar_t dest, xbt_dynar_t source)
57 {
58   xbt_dynar_reset(dest);
59
60   unsigned int cursor;
61   mc_comm_pattern_t comm;
62   xbt_dynar_foreach(source, cursor, comm) {
63     mc_comm_pattern_t copy_comm = MC_comm_pattern_dup(comm);
64     xbt_dynar_push(dest, &copy_comm);
65   }
66 }
67
68 void MC_restore_communications_pattern(mc_state_t state)
69 {
70   mc_list_comm_pattern_t list_process_comm;
71   unsigned int cursor;
72
73   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
74     list_process_comm->index_comm = (int)xbt_dynar_get_as(state->index_comm, cursor, int);
75   }
76
77   for (int i = 0; i < MC_smx_get_maxpid(); i++) {
78     MC_patterns_copy(
79       xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t),
80       xbt_dynar_get_as(state->incomplete_comm_pattern, i, xbt_dynar_t)
81     );
82   }
83 }
84
85 void MC_state_copy_incomplete_communications_pattern(mc_state_t state)
86 {
87   state->incomplete_comm_pattern = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
88
89   int i;
90   for (i=0; i < MC_smx_get_maxpid(); i++) {
91     xbt_dynar_t comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
92     xbt_dynar_t copy = MC_comm_patterns_dup(comms);
93     xbt_dynar_insert_at(state->incomplete_comm_pattern, i, &copy);
94   }
95 }
96
97 void MC_state_copy_index_communications_pattern(mc_state_t state)
98 {
99   state->index_comm = xbt_dynar_new(sizeof(unsigned int), NULL);
100   mc_list_comm_pattern_t list_process_comm;
101   unsigned int cursor;
102   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
103     xbt_dynar_push_as(state->index_comm, unsigned int, list_process_comm->index_comm);
104   }
105 }
106
107 void MC_handle_comm_pattern(
108   e_mc_call_type_t call_type, smx_simcall_t req,
109   int value, xbt_dynar_t pattern, int backtracking)
110 {
111
112   switch(call_type) {
113   case MC_CALL_TYPE_NONE:
114     break;
115   case MC_CALL_TYPE_SEND:
116   case MC_CALL_TYPE_RECV:
117     MC_get_comm_pattern(pattern, req, call_type, backtracking);
118     break;
119   case MC_CALL_TYPE_WAIT:
120   case MC_CALL_TYPE_WAITANY:
121     {
122       smx_synchro_t comm_addr = NULL;
123       if (call_type == MC_CALL_TYPE_WAIT)
124         comm_addr = simcall_comm_wait__get__comm(req);
125       else
126         // comm_addr = REMOTE(xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t)):
127         MC_process_read_dynar_element(&mc_model_checker->process, &comm_addr,
128           simcall_comm_waitany__get__comms(req), value, sizeof(comm_addr));
129       MC_complete_comm_pattern(pattern, comm_addr,
130         MC_smx_simcall_get_issuer(req)->pid, backtracking);
131     }
132     break;
133   default:
134     xbt_die("Unexpected call type %i", (int)call_type);
135   }
136
137 }
138
139 void MC_comm_pattern_free(mc_comm_pattern_t p)
140 {
141   xbt_free(p->rdv);
142   xbt_free(p->data);
143   xbt_free(p);
144   p = NULL;
145 }
146
147 static void MC_list_comm_pattern_free(mc_list_comm_pattern_t l)
148 {
149   xbt_dynar_free(&(l->list));
150   xbt_free(l);
151   l = NULL;
152 }
153
154 void MC_comm_pattern_free_voidp(void *p)
155 {
156   MC_comm_pattern_free((mc_comm_pattern_t) * (void **) p);
157 }
158
159 void MC_list_comm_pattern_free_voidp(void *p)
160 {
161   MC_list_comm_pattern_free((mc_list_comm_pattern_t) * (void **) p);
162 }