File: XdmfDsmBuffer.cxx

package info (click to toggle)
vtk6 6.1.0+dfsg2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 165,164 kB
  • ctags: 226,428
  • sloc: cpp: 1,354,490; ansic: 730,748; python: 227,134; tcl: 48,285; xml: 8,290; yacc: 4,832; java: 3,827; perl: 3,108; lex: 1,809; sh: 1,437; asm: 471; makefile: 229
file content (444 lines) | stat: -rw-r--r-- 16,674 bytes parent folder | download | duplicates (6)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*******************************************************************/
/*                               XDMF                              */
/*                   eXtensible Data Model and Format              */
/*                                                                 */
/*  Id : Id  */
/*  Date : $Date$ */
/*  Version : $Revision$ */
/*                                                                 */
/*  Author:                                                        */
/*     Jerry A. Clarke                                             */
/*     clarke@arl.army.mil                                         */
/*     US Army Research Laboratory                                 */
/*     Aberdeen Proving Ground, MD                                 */
/*                                                                 */
/*     Copyright @ 2007 US Army Research Laboratory                */
/*     All Rights Reserved                                         */
/*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
/*                                                                 */
/*     This software is distributed WITHOUT ANY WARRANTY; without  */
/*     even the implied warranty of MERCHANTABILITY or FITNESS     */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
/*     for more information.                                       */
/*                                                                 */
/*******************************************************************/
#include "XdmfDsmBuffer.h"
#include "XdmfDsmComm.h"
#include "XdmfDsmMsg.h"
#include "XdmfArray.h"
#include "XdmfExport.h"

#include <cstring>

#define XDMF_DSM_OPCODE_PUT     0x01
#define XDMF_DSM_OPCODE_GET     0x02
#define XDMF_DSM_SEMA_AQUIRE    0x03
#define XDMF_DSM_SEMA_RELEASE   0x04


extern "C"{
XDMF_EXPORT void *
XdmfDsmBufferServiceThread(void *DsmObj){
    XdmfDsmBuffer *Dsm = (XdmfDsmBuffer *)DsmObj;
    return(Dsm->ServiceThread());
}
}



XdmfDsmBuffer::XdmfDsmBuffer() {
    XdmfInt64 i;
    this->ThreadDsmReady = 0;
    this->DataPointer = 0;
    this->Locks = new XdmfInt64[XDMF_DSM_MAX_LOCKS];
    for(i=0;i < XDMF_DSM_MAX_LOCKS;i++) this->Locks[i] = -1;
}

XdmfDsmBuffer::~XdmfDsmBuffer() {
    if(this->StorageIsMine) delete[] this->Locks;
}

/*
XdmfInt32
XdmfDsmBuffer::Copy(XdmfDsmBuffer *Source){
    cout << "Copying" << endl;
    if(XdmfDsm::Copy((XdmfDsm *)Source) != XDMF_SUCCESS) return(XDMF_FAIL);
    cout << "Setting locks to " << Source->Locks << endl;
    this->Locks = Source->Locks;
    return(XDMF_SUCCESS);
}
*/

void *
XdmfDsmBuffer::ServiceThread(){
    XdmfInt32   ReturnOpcode;
    // Create a copy of myself to get a Unique XdmfDsmMessage
    XdmfDsmBuffer   UniqueBuffer;

    if (UniqueBuffer.Locks) delete[] UniqueBuffer.Locks;
    UniqueBuffer.Copy(this);
    XdmfDebug("Starting DSM Service on node " << UniqueBuffer.GetComm()->GetId());
    this->ThreadDsmReady = 1;
    UniqueBuffer.ServiceLoop(&ReturnOpcode);
    this->ThreadDsmReady = 0;
    XdmfDebug("Ending DSM Service on node " << UniqueBuffer.GetComm()->GetId() << " last op = " << ReturnOpcode);
    return((void *)this);
}

XdmfInt32
XdmfDsmBuffer::ServiceInit(){
    XdmfInt32   status = XDMF_SUCCESS;

    return(status);
}

XdmfInt32
XdmfDsmBuffer::ServiceOnce(XdmfInt32 *ReturnOpcode){
    XdmfInt32   status = XDMF_FAIL;

    this->Msg->SetTag(XDMF_DSM_COMMAND_TAG);
    status = this->Comm->Check(this->Msg);
    if(status != XDMF_SUCCESS){
        // Nothing to do
        return(XDMF_SUCCESS);
    }
    // Service One Call
    // cout << ".... Service a Call" << endl;
    return(this->Service(ReturnOpcode));
}

XdmfInt32
XdmfDsmBuffer::ServiceUntilIdle(XdmfInt32 *ReturnOpcode){
    XdmfInt32   status = XDMF_SUCCESS;

    while(status == XDMF_SUCCESS){
        this->Msg->SetTag(XDMF_DSM_COMMAND_TAG);
        status = this->Comm->Check(this->Msg);
        if(status != XDMF_SUCCESS){
            // Nothing to do
            return(XDMF_SUCCESS);
        }
        // Service One Call
        status = this->Service(ReturnOpcode);
        if(status != XDMF_SUCCESS){
            XdmfErrorMessage("ServiceUntilIdle detected error in Service() Method");
            return(XDMF_FAIL);
        }
    }
    return(XDMF_SUCCESS);
}

XdmfInt32
XdmfDsmBuffer::ServiceLoop(XdmfInt32 *ReturnOpcode){
    XdmfInt32   op, status = XDMF_SUCCESS;

    while(status == XDMF_SUCCESS){
        status = this->Service(&op);
        if(status != XDMF_SUCCESS) return(XDMF_FAIL);
        if(ReturnOpcode) *ReturnOpcode = op;
        if(op == XDMF_DSM_OPCODE_DONE) return(XDMF_SUCCESS);
    }
    return(XDMF_SUCCESS);
}

XdmfInt32
XdmfDsmBuffer::Service(XdmfInt32 *ReturnOpcode){
    XdmfInt32   Opcode, who, value, status = XDMF_FAIL;
    XdmfInt64   aLength, Address;
    XdmfByte    *datap;

    status = this->ReceiveCommandHeader(&Opcode, &who, &Address, &aLength);
    if(status == XDMF_FAIL){
        XdmfErrorMessage("Error Receiving Command Header");
        return(XDMF_FAIL);
    }
    switch(Opcode){
        case XDMF_DSM_OPCODE_PUT :
            XdmfDebug("PUT request from " << who << " for " << aLength << " bytes @ " << Address);
            if(aLength > (this->EndAddress - Address + 1)){
                XdmfErrorMessage("Length too long");
                return(XDMF_FAIL);
            }
            // datap = (XdmfByte *)this->Storage->GetDataPointer();
            // Stay out of HDF library. If it is threadsafe if will
            // deadlock on mpi_recv. If it is not threadsafe it will
            // get corrupted
            datap = this->DataPointer;
            datap += Address - this->StartAddress;
            this->Msg->SetTag(XDMF_DSM_COMMAND_TAG);
            status = this->ReceiveData(who, datap, aLength); 
            if(status == XDMF_FAIL){
                XdmfErrorMessage("ReceiveData() failed");
                return(XDMF_FAIL);
            }
            XdmfDebug("Serviced PUT request from " << who << " for " << aLength << " bytes @ " << Address);
            break;
        case XDMF_DSM_OPCODE_GET :
            XdmfDebug("(Server " << this->Comm->GetId() << ") Get request from " << who << " for " << aLength << " bytes @ " << Address);
            if(aLength > (this->EndAddress - Address + 1)){
                XdmfErrorMessage("Length " << aLength << " too long for address of len " << this->EndAddress - Address);
                XdmfErrorMessage("Server Start = " << this->StartAddress << " End = " << this->EndAddress);
                return(XDMF_FAIL);
            }
            // datap = (XdmfByte *)this->Storage->GetDataPointer();
            // Stay out of HDF library. If it is threadsafe if will
            // deadlock on mpi_recv. If it is not threadsafe it will
            // get corrupted
            datap = this->DataPointer;
            datap += Address - this->StartAddress;
            this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
            status = this->SendData(who, datap, aLength); 
            if(status == XDMF_FAIL){
                XdmfErrorMessage("SendData() failed");
                return(XDMF_FAIL);
            }
            XdmfDebug("(Server " << this->Comm->GetId() << ") Serviced GET request from " << who << " for " << aLength << " bytes @ " << Address);
            break;
        case XDMF_DSM_SEMA_AQUIRE :
            // cout << "Sema " << Address << " Aquire" << endl;
            if ((Address < 0) || (Address >= XDMF_DSM_MAX_LOCKS)){
                XdmfErrorMessage("Invalid Sema Request " << Address);
                value = XDMF_FAIL;
            }else{
                // cout << "Remote Locks at " << this->Locks << endl;
                // cout << "Server Locks[" << Address << "] = " << this->Locks[Address] << endl;
                if(this->Locks[Address] == -1){
                    // cout << "Remote " << who << " Aquired Lock " << Address << endl;
                    this->Locks[Address] = who;
                    value = XDMF_SUCCESS;
                }else{
                    // cout << "Remote " << who << " did not Aquired Lock " << Address << " already locked by " << this->Locks[Address] << endl;
                    value = XDMF_FAIL;
                }
            }
            this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
            status = this->SendData(who, (XdmfByte *)&value, sizeof(XdmfInt32));
            if(status == XDMF_FAIL){
                XdmfErrorMessage("SemaAquire Response Failed");
                return(XDMF_FAIL);
            }
            break;
        case XDMF_DSM_SEMA_RELEASE :
            // cout << "Sema " << Address << " Release" << endl;
            if ((Address < 0) || (Address >= XDMF_DSM_MAX_LOCKS)){
                XdmfErrorMessage("Invalid Sema Request " << Address);
                value = XDMF_FAIL;
            }else{
                if(this->Locks[Address] == who){
                    // cout << who << " Released Lock " << Address << endl;
                    this->Locks[Address] = -1;
                    value = XDMF_SUCCESS;
                }else{
                    // cout << who << " did not Release Lock " << Address << " already locked by " << this->Locks[Address] << endl;
                    value = XDMF_FAIL;
                }
            }
            this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
            status = this->SendData(who, (XdmfByte *)&value, sizeof(XdmfInt32));
            if(status == XDMF_FAIL){
                XdmfErrorMessage("SemaAquire Response Failed");
                return(XDMF_FAIL);
            }
            break;
        case XDMF_DSM_OPCODE_DONE :
            break;
        default :
            XdmfErrorMessage("Unknown Opcode " << Opcode);
            return(XDMF_FAIL);
    }
    if(ReturnOpcode) *ReturnOpcode = Opcode;
    return(XDMF_SUCCESS);
}
XdmfInt32
XdmfDsmBuffer::Aquire(XdmfInt64 Index){
    XdmfInt32   who, MyId = this->Comm->GetId();
    XdmfInt32   RemoteStatus;

    who = this->AddressToId(0);
    // cout << "Aquire :: MyId = " << MyId << " who = " << who << endl;
    if(who == XDMF_FAIL){
        XdmfErrorMessage("Address Error");
        return(XDMF_FAIL);
    }
    if ((Index < 0) || (Index >= XDMF_DSM_MAX_LOCKS)){
        XdmfErrorMessage("Invalid Sema Request " << Index);
        return(XDMF_FAIL);
    }
    if(who == MyId){
                // cout << "Local Locks at " << this->Locks << endl;
                // cout << "Local Locks[" << Index << "] = " << this->Locks[Index] << endl;
        if((this->Locks[Index] == -1) || (this->Locks[Index] == MyId)){
            this->Locks[Index] = MyId;
            // cout << who << " Aquired own lock " << Index << endl;
            return(XDMF_SUCCESS);
        }else{
            // cout << who << " did not Aquired own lock " << Index << endl;
            return(XDMF_FAIL);
        }
    }else{
        XdmfInt32   status;

        // cout << "Sending Header" << endl;
        status = this->SendCommandHeader(XDMF_DSM_SEMA_AQUIRE, who, Index, sizeof(XdmfInt64));
        if(status == XDMF_FAIL){
            XdmfErrorMessage("Failed to send Aquire Header to " << who);
            return(XDMF_FAIL);
        }
        this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
        // cout << "Getting Response" << endl;
        status = this->ReceiveData(who, &RemoteStatus, sizeof(XdmfInt32));
        if(status == XDMF_FAIL){
            XdmfErrorMessage("Failed to Aquire " << Index << " Response From " << who);
            return(XDMF_FAIL);
        }
        // cout << "RemoteStatus = " << RemoteStatus << endl;
        return(RemoteStatus);
    }
    return(XDMF_FAIL);
}

XdmfInt32
XdmfDsmBuffer::Release(XdmfInt64 Index){
    XdmfInt32   who, MyId = this->Comm->GetId();
    XdmfInt32   RemoteStatus;

    who = this->AddressToId(0);
    if(who == XDMF_FAIL){
        XdmfErrorMessage("Address Error");
        return(XDMF_FAIL);
    }
    if ((Index < 0) || (Index >= XDMF_DSM_MAX_LOCKS)){
        XdmfErrorMessage("Invalid Sema Request " << Index);
        return(XDMF_FAIL);
    }
    if(who == MyId){
        if((this->Locks[Index] == -1) || (this->Locks[Index] == MyId)){
            this->Locks[Index] = -1;
            //cout << who << " Released own lock " << Index << endl;
            return(XDMF_SUCCESS);
        }else{
            // cout << who << " did not Released own lock " << Index << endl;
            return(XDMF_FAIL);
        }
    }else{
        XdmfInt32   status;

        // cout << "Sending Release Header" << endl;
        status = this->SendCommandHeader(XDMF_DSM_SEMA_RELEASE, who, Index, sizeof(XdmfInt64));
        if(status == XDMF_FAIL){
            XdmfErrorMessage("Failed to send Release Header to " << who);
            return(XDMF_FAIL);
        }
        this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
        // cout << "Receiving Release Response " << endl;
        status = this->ReceiveData(who, &RemoteStatus, sizeof(XdmfInt32));
        if(status == XDMF_FAIL){
            XdmfErrorMessage("Failed to Release " << Index << " Response From " << who);
            return(XDMF_FAIL);
        }
        // cout << "Release Response = " << RemoteStatus << endl;
        return(RemoteStatus);
    }
    return(XDMF_FAIL);
}

XdmfInt32
XdmfDsmBuffer::Put(XdmfInt64 Address, XdmfInt64 aLength, void *Data){
    XdmfInt32   who, MyId = this->Comm->GetId();
    XdmfInt64   astart, aend, len;
    XdmfByte    *datap = (XdmfByte *)Data;

    while(aLength){
        who = this->AddressToId(Address);
        if(who == XDMF_FAIL){
            XdmfErrorMessage("Address Error");
            return(XDMF_FAIL);
        }
        this->GetAddressRangeForId(who, &astart, &aend);
        len = MIN(aLength, aend - Address + 1);
        XdmfDebug("Put " << len << " Bytes to Address " << Address << " Id = " << who);
        if(who == MyId){
            XdmfByte *dp;

            // cout << "That's me!!" << endl;
            // dp = (XdmfByte *)this->Storage->GetDataPointer();
            // Stay out of HDF library. If it is threadsafe if will
            // deadlock on mpi_recv. If it is not threadsafe it will
            // get corrupted
            dp = this->DataPointer;
            dp += Address - this->StartAddress;
            memcpy(dp, datap, len);

        }else{
            XdmfInt32   status;

            status = this->SendCommandHeader(XDMF_DSM_OPCODE_PUT, who, Address, len);
            if(status == XDMF_FAIL){
                XdmfErrorMessage("Failed to send PUT Header to " << who);
                return(XDMF_FAIL);
            }
            this->Msg->SetTag(XDMF_DSM_COMMAND_TAG);
            status = this->SendData(who, datap, len);
            if(status == XDMF_FAIL){
                XdmfErrorMessage("Failed to send " << len << " bytes of data to " << who);
                return(XDMF_FAIL);
            }

        }
        aLength -= len;
        Address += len;
        datap += len;
    }
    return(XDMF_SUCCESS);
}

XdmfInt32
XdmfDsmBuffer::Get(XdmfInt64 Address, XdmfInt64 aLength, void *Data){
    XdmfInt32   who, MyId = this->Comm->GetId();
    XdmfInt64   astart, aend, len;
    XdmfByte    *datap = (XdmfByte *)Data;

    while(aLength){
        who = this->AddressToId(Address);
        if(who == XDMF_FAIL){
            XdmfErrorMessage("Address Error");
            return(XDMF_FAIL);
        }
        this->GetAddressRangeForId(who, &astart, &aend);
        len = MIN(aLength, aend - Address + 1);
        XdmfDebug("Get " << len << " Bytes from Address " << Address << " Id = " << who);
        if(who == MyId){
            XdmfByte *dp;

            // cout << "That's me!!" << endl;
            // dp = (XdmfByte *)this->Storage->GetDataPointer();
            // Stay out of HDF library. If it is threadsafe if will
            // deadlock on mpi_recv. If it is not threadsafe it will
            // get corrupted
            dp = this->DataPointer;
            dp += Address - this->StartAddress;
            memcpy(datap, dp, len);

        }else{
            XdmfInt32   status;

            status = this->SendCommandHeader(XDMF_DSM_OPCODE_GET, who, Address, len);
            if(status == XDMF_FAIL){
                XdmfErrorMessage("Failed to send GET Header to " << who);
                return(XDMF_FAIL);
            }
            this->Msg->SetTag(XDMF_DSM_RESPONSE_TAG);
            status = this->ReceiveData(who, datap, len);
            if(status == XDMF_FAIL){
                XdmfErrorMessage("Failed to receive " << len << " bytes of data from " << who);
                return(XDMF_FAIL);
            }

        }
        aLength -= len;
        Address += len;
        datap += len;
    }
    return(XDMF_SUCCESS);
}