Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add proper copyright headers to the MC files
[simgrid.git] / src / mc / mc_state.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "../simix/smx_private.h"
7 #include "xbt/fifo.h"
8 #include "mc_private.h"
9
10 /**
11  * \brief Creates a state data structure used by the exploration algorithm
12  */
13 mc_state_t MC_state_new(void)
14 {
15   mc_state_t state = NULL;
16   
17   state = xbt_new0(s_mc_state_t, 1);
18   state->max_pid = simix_process_maxpid;
19   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
20   
21   mc_stats->expanded_states++;
22   return state;
23 }
24
25 mc_state_t MC_state_pair_new(void)
26 {
27   mc_state_t state = NULL;
28   
29   state = xbt_new0(s_mc_state_t, 1);
30   state->max_pid = simix_process_maxpid;
31   state->proc_status = xbt_new0(s_mc_procstate_t, state->max_pid);
32   
33   //mc_stats->expanded_states++;
34   return state;
35 }
36
37 /**
38  * \brief Deletes a state data structure
39  * \param trans The state to be deleted
40  */
41 void MC_state_delete(mc_state_t state)
42 {
43   xbt_free(state->proc_status);
44   xbt_free(state);
45 }
46
47 void MC_state_interleave_process(mc_state_t state, smx_process_t process)
48 {
49   state->proc_status[process->pid].state = MC_INTERLEAVE;
50   state->proc_status[process->pid].interleave_count = 0;
51 }
52
53 unsigned int MC_state_interleave_size(mc_state_t state)
54 {
55   unsigned int i, size=0;
56
57   for(i=0; i < state->max_pid; i++){
58     if(state->proc_status[i].state == MC_INTERLEAVE)
59       size++;
60   }
61
62   return size;
63 }
64
65 int MC_state_process_is_done(mc_state_t state, smx_process_t process){
66   return state->proc_status[process->pid].state == MC_DONE ? TRUE : FALSE;
67 }
68
69 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value)
70 {
71   state->executed_req = *req;
72   state->req_num = value;
73
74   /* The waitany and testany request are transformed into a wait or test request over the
75    * corresponding communication action so it can be treated later by the dependence
76    * function. */
77   switch(req->call){
78     case SIMCALL_COMM_WAITANY:
79       state->internal_req.call = SIMCALL_COMM_WAIT;
80       state->internal_req.issuer = req->issuer;
81       state->internal_comm = *xbt_dynar_get_as(req->comm_waitany.comms, value, smx_action_t);
82       state->internal_req.comm_wait.comm = &state->internal_comm;
83       state->internal_req.comm_wait.timeout = 0;
84       break;
85
86     case SIMCALL_COMM_TESTANY:
87       state->internal_req.call = SIMCALL_COMM_TEST;
88       state->internal_req.issuer = req->issuer;
89
90       if(value > 0)
91         state->internal_comm = *xbt_dynar_get_as(req->comm_testany.comms, value, smx_action_t);
92
93       state->internal_req.comm_wait.comm = &state->internal_comm;
94       state->internal_req.comm_test.result = value;
95       break;
96
97     case SIMCALL_COMM_WAIT:
98       state->internal_req = *req;
99       state->internal_comm = *(req->comm_wait.comm);
100       state->executed_req.comm_wait.comm = &state->internal_comm;
101       state->internal_req.comm_wait.comm = &state->internal_comm;
102       break;
103
104     case SIMCALL_COMM_TEST:
105       state->internal_req = *req;
106       state->internal_comm = *req->comm_test.comm;
107       state->executed_req.comm_test.comm = &state->internal_comm;
108       state->internal_req.comm_test.comm = &state->internal_comm;
109       break;
110
111     default:
112       state->internal_req = *req;
113       break;
114   }
115 }
116
117 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value)
118 {
119   *value = state->req_num;
120   return &state->executed_req;
121 }
122
123 smx_simcall_t MC_state_get_internal_request(mc_state_t state)
124 {
125   return &state->internal_req;
126 }
127
128 smx_simcall_t MC_state_get_request(mc_state_t state, int *value)
129 {
130   smx_process_t process = NULL;
131   mc_procstate_t procstate = NULL;
132   unsigned int start_count;
133
134   xbt_swag_foreach(process, simix_global->process_list){
135     procstate = &state->proc_status[process->pid];
136
137     if(procstate->state == MC_INTERLEAVE){
138       if(MC_process_is_enabled(process)){
139         switch(process->simcall.call){
140           case SIMCALL_COMM_WAITANY:
141             *value = -1;
142             while(procstate->interleave_count < xbt_dynar_length(process->simcall.comm_waitany.comms)){
143               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
144                 *value = procstate->interleave_count-1;
145                 break;
146               }
147             }
148
149             if(procstate->interleave_count >= xbt_dynar_length(process->simcall.comm_waitany.comms))
150               procstate->state = MC_DONE;
151
152             if(*value != -1)
153               return &process->simcall;
154
155             break;
156
157           case SIMCALL_COMM_TESTANY:
158             start_count = procstate->interleave_count;
159             *value = -1;
160             while(procstate->interleave_count < xbt_dynar_length(process->simcall.comm_testany.comms)){
161               if(MC_request_is_enabled_by_idx(&process->simcall, procstate->interleave_count++)){
162                 *value = procstate->interleave_count - 1;
163                 break;
164               }
165             }
166
167             if(procstate->interleave_count >= xbt_dynar_length(process->simcall.comm_testany.comms))
168               procstate->state = MC_DONE;
169
170             if(*value != -1 || start_count == 0)
171               return &process->simcall;
172
173             break;
174
175           case SIMCALL_COMM_WAIT:
176             if(process->simcall.comm_wait.comm->comm.src_proc
177                && process->simcall.comm_wait.comm->comm.dst_proc){
178               *value = 0;
179             }else{
180               *value = -1;
181             }
182             procstate->state = MC_DONE;
183             return &process->simcall;
184
185             break;
186
187           default:
188             procstate->state = MC_DONE;
189             *value = 0;
190             return &process->simcall;
191             break;
192         }
193       }
194     }
195   }
196
197   return NULL;
198 }