darshan-mpi-datatypes.c 8.16 KB
Newer Older
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
/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*  
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
//#include "mpi.h"
#include "adio.h"
#include "adio_extern.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>



ADIOI_Flatlist_node * get_flatlist(MPI_Datatype dt){
      int is_contig;
      ADIOI_Datatype_iscontig(dt,&is_contig);
      if (is_contig)
	  return NULL;
      else {
	  ADIOI_Flatlist_node *flat_buf;
	  ADIOI_Flatten_datatype(dt);
	  flat_buf = ADIOI_Flatlist;
	  while ((flat_buf)&&(flat_buf->type != dt)) flat_buf = flat_buf->next; 
	  if (flat_buf) {
	      if (flat_buf->incr_size[0] == -1) {
		  int i;
		  flat_buf->incr_size[0] = 0;
		  for (i=1;i<flat_buf->count;i++)
		      flat_buf->incr_size[i] = flat_buf->incr_size[i-1] + flat_buf->blocklens[i-1]; 
	      }
	  }
	  return flat_buf;
      }
}
 
void print_flatlist(MPI_Datatype dt) {
  int b_index;
  ADIOI_Flatlist_node *flat_buf;
  flat_buf = get_flatlist(dt);
  if (flat_buf)
    for (b_index=0; b_index < flat_buf->count; b_index++) {
	printf ("%3d-th: offset=%8lld len=%8lld \n",b_index, flat_buf->indices[b_index],flat_buf->blocklens[b_index]);
    }
  else
    printf ("NULL flat_buf\n");
}

void print_flatlist2(ADIOI_Flatlist_node *flat_buf) {
  int b_index;
  if (flat_buf)
    for (b_index=0; b_index < flat_buf->count; b_index++) {
	printf ("%3d-th: offset=%8lld len=%8lld \n",b_index, flat_buf->indices[b_index],flat_buf->blocklens[b_index]);
    }
  else
    printf ("NULL flat_buf\n");
}


#define LEFT 0
#define RIGHT 1

// assumes : x > 0
// result:  between 0 and flat_buf->count inclusively
// returns the index of the flat_buf where x is located
// LEFT
//  ____***____***____****--
//  000000011111112222222233
//  RIGHT
//  000011111112222222333333 

int find_bin_search(MPI_Offset x, MPI_Datatype datatype, int leftright){
    ADIOI_Flatlist_node *flat_buf = get_flatlist(datatype);
    if (!flat_buf) return 0;

    int l = 0 ,r = flat_buf->count - 1 ,found = -1;
    while (l <= r) {
	int m = (l + r) / 2;
	if (x < flat_buf->indices[m] - flat_buf->indices[0] + flat_buf->blocklens[m]) {
	    if (l == m) {
		found = m;
		break;
	    }
	    if (x >= flat_buf->indices[m - 1] - flat_buf->indices[0] + flat_buf->blocklens[m - 1]) {
		found = m;
		break;
	    }
	    r = m - 1 ;
	}
	else {
	    if (m == r) {
		found = m +1;
		//found = m;
		break;
	    }
	    if (x < flat_buf->indices[m + 1] - flat_buf->indices[0]) {
		found = m + 1;
		//found = m;
		break;
	    }
	    l = m +1;
	}
    }
    //    return found - ((flat_buf->blocklens[0] == 0)?1:0);
    if ((leftright == RIGHT) 
	&& (found < flat_buf->count)
	&& (x < flat_buf->indices[found] - flat_buf->indices[0] + flat_buf->blocklens[found]) 
	&& (x >= flat_buf->indices[found] - flat_buf->indices[0])) found++;
    return found;
}

// Finds the file offset within the extent range  
// assumes : x > 0
int find_size_bin_search(MPI_Offset x, ADIOI_Flatlist_node *flat_buf){
  int l = 0 ,r = flat_buf->count - 1 ,size = -1;
  while (l <= r) {
    int m = (l + r) / 2;
    //printf("\n*** l=%d r=%d\n",l,r);
    if (x < flat_buf->incr_size[m] + flat_buf->blocklens[m]) {
      if (l == m) {
	size = flat_buf->indices[m] + x - flat_buf->incr_size[m];
	break;
      }
      if (x >= flat_buf->incr_size[m]) {
	size = flat_buf->indices[m] + x - flat_buf->incr_size[m];
	break;
      }
      r = m - 1 ;
    }
    else {
	 // ???
	if (m == r) {
	    size = flat_buf->indices[m] + x - flat_buf->incr_size[m];
	    break;
	}
	if (x < flat_buf->incr_size[m + 1] + flat_buf->blocklens[m + 1]) {
	    size = flat_buf->indices[m + 1] + x - flat_buf->incr_size[m + 1];
	    break;
	}
	l = m +1;
    }
  }
  return size;
}

void free_datatype(MPI_Datatype type) {
    int ni, na, nd, combiner; 
    MPI_Type_get_envelope(type, &ni, &na, &nd, &combiner); 
    if (combiner != MPI_COMBINER_NAMED)
	MPI_Type_free(&type);
}

/*int my_get_view(MPI_File fh, MPI_Offset *disp, MPI_Datatype *etype, MPI_Datatype *filetype){
    ADIO_File adio_fh = MPIO_File_resolve(fh);
    *disp = adio_fh->disp;
    *etype =  adio_fh->etype;
    *filetype = adio_fh->filetype;
    return MPI_SUCCESS;
}
*/

MPI_Offset func_1_per( MPI_Offset x, ADIOI_Flatlist_node *flat_buf){
  return  find_size_bin_search(x, flat_buf) - flat_buf->indices[0];
}

/* Maps a view offset on a file offset */

MPI_Offset func_1(MPI_File fh, MPI_Offset x){
    int is_contig, etype_size;
    MPI_Offset disp_plus_lb, ret;
    MPI_Datatype etype, filetype;
    char datarep[MPI_MAX_DATAREP_STRING];
    MPI_Aint lb, extent;
    
    MPI_File_get_view(fh, &disp_plus_lb, &etype, &filetype, datarep);
    //my_get_view(fh, &disp_plus_lb, &etype, &filetype);
    MPI_Type_get_extent(filetype, &lb, &extent);
    MPI_Type_size(etype, &etype_size);
    
    x = x * etype_size; 
    
    disp_plus_lb += lb;
    ADIOI_Datatype_iscontig(filetype, &is_contig);
    if (is_contig) 
	ret = x + disp_plus_lb;
    else {
	int sz;
	MPI_Type_size(filetype, &sz);
	ret = disp_plus_lb + x / sz * extent + func_1_per(x % sz, get_flatlist(filetype));
    }
    free_datatype(etype);
    free_datatype(filetype);
    return ret;
}

/* Maps a view offset on a file offset: 
trasforms first etype -> bytes 
and then moves on the previous byte */

MPI_Offset func_1_inf(MPI_File fh, MPI_Offset x, int memtype_size){
    int is_contig, etype_size;
    MPI_Offset disp_plus_lb, ret;
    MPI_Datatype etype, filetype;
    char datarep[MPI_MAX_DATAREP_STRING];
    MPI_Aint lb, extent;
    
    MPI_File_get_view(fh, &disp_plus_lb, &etype, &filetype, datarep);
    //my_get_view(fh, &disp_plus_lb, &etype, &filetype);
    MPI_Type_get_extent(filetype, &lb, &extent);
    MPI_Type_size(etype, &etype_size);
    
    x = x * etype_size + memtype_size - 1; 
    
    disp_plus_lb += lb;
    ADIOI_Datatype_iscontig(filetype, &is_contig);
    if (is_contig) 
	ret = x + disp_plus_lb;
    else {
	int sz;
	MPI_Type_size(filetype, &sz);
	ret = disp_plus_lb + x / sz * extent + func_1_per(x % sz, get_flatlist(filetype));
    }
    free_datatype(etype);
    free_datatype(filetype);
    return ret;
}


/* This version compacts the neighboring contiguous blocks */ 
int count_contiguous_blocks_memory(MPI_Datatype datatype, int count) {
    ADIOI_Flatlist_node *flat_buf;
    flat_buf = get_flatlist(datatype);
    return (flat_buf)?(flat_buf->count*count):1;
}

#define BEGIN_BLOCK(x,block_size) (((x)/(block_size))*(block_size))
#define END_BLOCK(x,block_size) (((x)/(block_size)+1)*(block_size)-1)
#define BEGIN_NEXT_BLOCK(x,block_size) (((x)/(block_size)+1)*(block_size))


int count_contiguous_blocks_file(MPI_File fh, MPI_Offset foff1, MPI_Offset foff2) {
    MPI_Offset disp;
    MPI_Datatype etype, filetype;
    char datarep[MPI_MAX_DATAREP_STRING];
    int is_contig;
    int ret;

    assert(foff1 <= foff2);
     
    MPI_File_get_view(fh, &disp, &etype, &filetype, datarep);
    //my_get_view(fh, &disp, &etype, &filetype);
    ADIOI_Datatype_iscontig(filetype, &is_contig);
    if (is_contig)
	ret=1;
    else { 
	MPI_Aint extent, lb;
	ADIOI_Flatlist_node *flat_buf;
	flat_buf = get_flatlist(filetype);
	MPI_Type_get_extent(filetype, &lb, &extent);
	
	assert(disp+lb <= foff1);
	assert(lb == flat_buf->indices[0]);

	if (foff2 < ((flat_buf->blocklens[0] == 0)?flat_buf->indices[1]:flat_buf->indices[0]))
	    ret=0;
	else{
	    int ind1, ind2;
	    ind1 = find_bin_search((foff1 - disp - lb) % extent, filetype, LEFT);
	    ind2 = find_bin_search((foff2 - disp - lb) % extent, filetype, RIGHT);
	    
	    if ((foff2 - disp - lb) < BEGIN_NEXT_BLOCK(foff1 - disp - lb, extent)) 
		ret = ind2 - ind1;
	    else 
		ret = flat_buf->count - ind1 - ((flat_buf->blocklens[flat_buf->count-1] == 0)?1:0)+
		    (BEGIN_BLOCK(foff2 - disp - lb, extent)-
		     BEGIN_NEXT_BLOCK(foff1 - disp - lb, extent)) 
		    / extent * (flat_buf->count-
				((flat_buf->blocklens[0] == 0)?1:0) -
				((flat_buf->blocklens[flat_buf->count-1] == 0)?1:0)-
				(((flat_buf->blocklens[flat_buf->count-1])&&(flat_buf->blocklens[flat_buf->count-1] + flat_buf->indices[flat_buf->count-1] - flat_buf->indices[0] == extent ))?1:0))
		    + ind2 - (((ind2>0)&&(flat_buf->blocklens[0] == 0))?1:0) -  
		    (((ind2>0)&&(flat_buf->indices[0]==0))?1:0); 
	}
    }
    free_datatype(etype);
    free_datatype(filetype);
    return ret;
}