mw_addslave4.cpp 10 KB
Newer Older
Camille Coti's avatar
Camille Coti committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <iostream>
#include <mpi.h>
#include <ginac/ginac.h>
#include <math.h> // for ceil

#include "products.h"
#include "utils_parall.h"
#include "parall_constants.h"
#include "parall_internal.h"
#include "utils.h"
#include "profiling.h"

namespace gi = GiNaC;

#define MAXLENADD  1 // 256

unsigned int maxlen(  std::vector<std::string> expressions ){
    unsigned int len = 0;
    for( auto s: expressions  ) {
        unsigned int l2 = s.length();
        if( len < l2 ) {
            len = l2;
        }
    }
    return len;
}

gi::ex add_expressions_parall( std::vector<std::string> expressions, gi::lst symbols, parameters_2_1_t p, MPI_Comm comm = MPI_COMM_WORLD ) {
    gi::ex Tens = 0;
    int size, i, nb, len;
    unsigned int chunk, end;
    std::vector<unsigned int> cut;
    unsigned int* lengths;
    std::string result;
    char* expr;
    MPI_Status status;
    size_t expr_c_size = 0;
    char* expr_c;

    /* If the expressions are short, compute the sum locally */
    if( maxlen( expressions ) < MAXLENADD )
        return add_expressions( expressions, symbols );
    
    MPI_Comm_size( comm, &size );
    nb = expressions.size();
    lengths = (unsigned int*) malloc( nb * sizeof( unsigned int ) );
    for( i = 0 ; i < nb ; i++ ) {
        cut.push_back( 0 );
        lengths[i] = 0;
    }
    unsigned int running = size - 1;
    p.setParams( nb, 1 );

    /* TODO ca se factorise avec send_expressions_to_add */

    for( int peer = 1 ; peer < size ; peer++ ) {
    
        i = 0;
        for( auto s: expressions  ) {
            /* How much are we going to send: stop at a + or - sign (and keep the sign) */
            chunk = ceil( s.length() / ( size - 1 ) );
            end = cut[i] + chunk;
            while( !( s[end] == '+' || s[end] == '-' || end == s.length() - 1) ){
                end++;
            }
            end--;
           
            lengths[i] = end - cut[i] + 1;
            i++;
        }
        
        /* Send the lengths */
        MPI_Send( &p, 1, DT_PARAMETERS_2_1, peer, TAG_ADD, comm );
        MPI_Send( lengths, nb, MPI_INT, peer, TAG_ADD, comm );
        
        /* Send the strings */
        
        for( unsigned int j = 0 ; j < nb ; j++ ) {
            expr = const_cast<char*>( expressions[j].c_str() );
        
           MPI_Send( &( expr[ cut[j] ] ), lengths[j], MPI_CHAR, peer, TAG_ADD, comm );
           cut[j] += lengths[j];
        }
    }
    
    /* Receive the results */
    
    expr_c = NULL;

    while( running > 0 ) {
        MPI_Recv( &len, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, &status );
        int src = status.MPI_SOURCE;
        len++;
        
        if( len != 0 ) {
            
            if( len > expr_c_size ) {
                expr_c_size = len;
                if( NULL != expr_c ) free( expr_c );
                expr_c = (char*)malloc( expr_c_size ); // The \0 was added by the slave
            }

            /* Receive the result */
            MPI_Recv( expr_c, len-1, MPI_CHAR, src, TAG_EXPR, comm, &status );
            expr_c[len - 1] = '\n';
            
            /* Concatenate the result */
            std::string recvs( expr_c );
            if( expr_c[0] != '-' ) result += '+';
            result += recvs;
        }
        running--;
        send_end( src, p );
    }
        
    Tens = de_linearize_expression( result, symbols );
            
    free( lengths );
    free( expr_c );
    return Tens;
}

/*******************************************************************************
 *         Parallel 1-level decomposition with addition on a slave             *
 *******************************************************************************/

gi::ex multiply_1level_master_addslave4( tensor3D_t& T, unsigned int size, MPI_Comm comm = MPI_COMM_WORLD ) { 
    gi::ex Tens = 0;
    unsigned int a2, a4;
    gi::lst symbols;

    MPI_Status status;
    char* expr_c;
    size_t expr_c_size = 0;
    int src, np;
    unsigned int len, running = 0;
    parameters_2_1_t pzero( 0, 0 );

    MPI_Comm_size( comm, &np );

    expr_c = NULL;
    expr_c = (char*) malloc( 3279 );
    
    int receivedresults = 0;
    unsigned int N = size/2;

    std::vector<parameters_2_1_t> input;
    std::vector<std::string> results; /* length and char* */

    /* Build a list of argument sets */
    
    for( a4 = 0 ; a4 < N ; a4++ ){
        for( a2 = 0; a2 < N ; a2++ ){
            parameters_2_1_t p( a4, a2 );
            input.push_back( p );
	    }
	}

    /* Compute the set of symbols */
    /* Could be done while the first slave is working */
    
    symbols = all_symbols_3D( size );

    /* Distribute the work */

    while( input.size() > 0 ) {
        MPI_Recv( &len, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, &status );
        
        if( status.MPI_TAG == TAG_PULL ) {
            /* Nothing else will come: just send wome work */
            src = status.MPI_SOURCE;
            send_work( input, src );
            
        } else {
            if( status.MPI_TAG == TAG_RES ){
                src = status.MPI_SOURCE;

                /* The first message contains the length of what is coming next */
                if( len != 0 ) {
                    if( len > expr_c_size ) {
                        expr_c_size = len;
                        if( NULL != expr_c ) free( expr_c );
                        expr_c = (char*)malloc( expr_c_size ); // The \0 was added by the slave
                    }
                    
                    /* Receive the result */
                    MPI_Recv( expr_c, len, MPI_CHAR, src, TAG_EXPR, comm, &status );

                    /* Put it in the result queue */
                    results.push_back( std::string( expr_c ) );
                }                    
                    
                /* Send more work  */
                send_work_addslave( input, results, src );
            } else {
                std::cerr << "Wrong tag received " << status.MPI_TAG << std::endl;
            }
            
        }
   }

   /* Wait until everyone is done */

    running = np - 1; // all the slaves are running 
    while( running > 0 ) {
        MPI_Recv( &len, 1, MPI_UNSIGNED, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, &status );
        src = status.MPI_SOURCE;
        
        if( len != 0 ) {
            if( len > expr_c_size ) {
                expr_c_size = len;
                if( NULL != expr_c ) free( expr_c );
                expr_c = (char*)malloc( expr_c_size ); // The \0 was added by the slave
            }

            /* Receive the result */
            MPI_Recv( expr_c, len, MPI_CHAR, src, TAG_EXPR, comm, &status );

            /* Put it in the result queue */
            results.push_back( std::string( expr_c ) );
        }
        /* Do not send the end signal yet */
        running--;
    }

    /* Add whatever I have left */
    Tens = add_expressions_parall( results, symbols, pzero, comm );
    
#if DEBUG
    std::cout << "Received " << receivedresults << " results" << std::endl;

    std::cout << "Tpara=" << Tens << ";" << std::endl;
#endif
    
    if( NULL != expr_c) free( expr_c );
    return Tens;
}

void multiply_1level_slave_addslave4( tensor3D_t& T, unsigned int size, MPI_Comm comm = MPI_COMM_WORLD ) {
    gi::ex Tens;
    int  a2, a4;
    unsigned int len = 0;
    
    parameters_2_1_t params;
    MPI_Status status;
    char* expr_c;

    int rank;
    MPI_Comm_rank( comm, &rank );

    /* Ask for some work */
    
    MPI_Send( &len, 1, MPI_UNSIGNED, ROOT, TAG_PULL, comm );

    /* Compute the set of symbols */
    
    gi::lst symbols = all_symbols_3D( size );

    while( true ){
        /* Receive a set of parameters */

        MPI_Recv( &params, 1, DT_PARAMETERS_2_1, ROOT, MPI_ANY_TAG, comm, &status );
        
        if( status.MPI_TAG == TAG_WORK ){
            a4 = params.a4;
            a2 = params.a2;

            Tens = one_level1_product( &T, size, a4, a2 );

            send_result( Tens );

        } else {
            if( status.MPI_TAG == TAG_ADD ) {
                /* Receive a set of expressions to add */

                /* Number of expressions received */
                int nb = params.a4;
                a2 = params.a2;

                /* Length of each string */

                unsigned int* lengths = (unsigned int*) malloc( nb*sizeof( unsigned int ) );
                MPI_Recv( lengths, nb, MPI_INT, ROOT, TAG_ADD, comm, &status );
                std::vector<std::string> results_s;
                char* c_str;
                int i;
                int len;
                for( i = 0 ; i < nb ; i++ ) {
                    len = lengths[i] + 1 ;
                    c_str = (char*) malloc( len );
                    MPI_Recv( c_str, len - 1, MPI_CHAR, ROOT, TAG_ADD, comm, &status );
                    c_str[len - 1] = '\0';    // The master sends C++ strings, which do not contain the final '\0'
                    results_s.push_back( std::string( c_str ) );
                    free( c_str );
                }

                /* Delinearize all the expressions and add them */

                Tens = add_expressions( results_s, symbols );
                
                /* Send the result */

                send_result( Tens );

            } else {
                if( status.MPI_TAG == TAG_END ){
                    return;
                } else {
                    std::cerr << "Wrong tag received on slave " << status.MPI_TAG << std::endl;
                }
            }
        }
    }
}

/* Communication protocol:
   M -> W: always the same size, therefore unique communication
   W -> M: send an unsigned int (size of the expression), then the expression (table of chars)
*/
        
gi::ex multiply_1level_mw_addslave4( tensor3D_t& T, int size ) {  // simpler: same dimension everywhere
    int rank;
    gi::ex Tens = 0;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );

    /* Create a new datatype for the parameters */
    
    create_parameters_datatype_2_1();

    /* Here we go */

    if( 0 == rank ) {
        Tens = multiply_1level_master_addslave4( T, size );
    } else {
        multiply_1level_slave_addslave4( T, size );
    }

    /* Finalize */
    
    free_parameters_2_1_dt();
    return Tens;
}