1 | #include "xios_spl.hpp" |
---|
2 | #include "exception.hpp" |
---|
3 | #include "log.hpp" |
---|
4 | #include "buffer_out.hpp" |
---|
5 | #include "buffer_client.hpp" |
---|
6 | #include "cxios.hpp" |
---|
7 | #include "mpi.hpp" |
---|
8 | #include "tracer.hpp" |
---|
9 | |
---|
10 | namespace xios |
---|
11 | { |
---|
12 | |
---|
13 | size_t maxRequestSize=0 ; |
---|
14 | |
---|
15 | CClientBuffer::CClientBuffer(MPI_Comm interComm_,int serverRank_, StdSize bfSize) |
---|
16 | { |
---|
17 | bufferSizeByServer=bfSize; //CXios::bufferSize ; |
---|
18 | info(10)<<"bufferSizeByServer "<<bufferSizeByServer<<endl ; |
---|
19 | interComm=interComm_ ; |
---|
20 | serverRank=serverRank_ ; |
---|
21 | bufferSize=bufferSizeByServer; //2 ; |
---|
22 | buffer[0]=new char[bufferSize] ; // transform it with MPI_ALLOC_MEM later |
---|
23 | buffer[1]=new char[bufferSize] ; |
---|
24 | current=0 ; |
---|
25 | count=0 ; |
---|
26 | pending=false ; |
---|
27 | retBuffer=new CBufferOut(buffer[current],bufferSize) ; |
---|
28 | } |
---|
29 | |
---|
30 | CClientBuffer::~CClientBuffer() |
---|
31 | { |
---|
32 | delete [] buffer[0] ; |
---|
33 | delete [] buffer[1] ; |
---|
34 | delete retBuffer ; |
---|
35 | } |
---|
36 | |
---|
37 | int CClientBuffer::remain(void) |
---|
38 | { |
---|
39 | return bufferSize-count ; |
---|
40 | } |
---|
41 | |
---|
42 | bool CClientBuffer::isBufferFree(int size) |
---|
43 | { |
---|
44 | if (size>maxRequestSize) maxRequestSize=size ; |
---|
45 | |
---|
46 | if (size>bufferSize) ERROR("CClientBuffer::hasSpace(int size)", |
---|
47 | <<"request size is too big for buffer, increase buffer client size"<<endl |
---|
48 | <<"buffer_size must be > "<<size*2<<endl) |
---|
49 | |
---|
50 | if (size<=remain()) return true ; |
---|
51 | else return false ; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | CBufferOut* CClientBuffer::getBuffer(int size) |
---|
56 | { |
---|
57 | if (size<=remain()) |
---|
58 | { |
---|
59 | retBuffer->realloc(buffer[current]+count,size) ; |
---|
60 | count+=size ; |
---|
61 | return retBuffer ; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | ERROR("CBufferOut* CClientBuffer::getSpace(int size) ;", |
---|
66 | <<"No ennough space in buffer, that may not happen..."); |
---|
67 | return NULL ; |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | bool CClientBuffer::checkBuffer(void) |
---|
73 | { |
---|
74 | MPI_Status status ; |
---|
75 | int flag ; |
---|
76 | |
---|
77 | if (pending) |
---|
78 | { |
---|
79 | traceOff() ; |
---|
80 | MPI_Test(&request,&flag,&status) ; |
---|
81 | traceOn() ; |
---|
82 | if (flag==true) pending=false ; |
---|
83 | } |
---|
84 | |
---|
85 | if (!pending) |
---|
86 | { |
---|
87 | if (count>0) |
---|
88 | { |
---|
89 | MPI_Issend(buffer[current],count,MPI_CHAR,serverRank,20,interComm,&request) ; |
---|
90 | pending=true ; |
---|
91 | if (current==1) current=0 ; |
---|
92 | else current=1 ; |
---|
93 | count=0 ; |
---|
94 | } |
---|
95 | } |
---|
96 | return pending ; |
---|
97 | } |
---|
98 | |
---|
99 | bool CClientBuffer::hasPendingRequest(void) |
---|
100 | { |
---|
101 | if (pending) return true ; |
---|
102 | else if (count>0) return true ; |
---|
103 | else return false ; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | } |
---|
109 | |
---|