Fork me on GitHub

source: svn/trunk/Utilities/mcfio/mcfio_Block.c@ 654

Last change on this file since 654 was 3, checked in by Xavier Rouby, 16 years ago

first commit

File size: 17.2 KB
RevLine 
[3]1/*******************************************************************************
2* *
3* mcfio_Block.c -- Utility routines for the McFast Monte-Carlo *
4* The routine to encode/decode a block *
5* *
6* Copyright (c) 1994 Universities Research Association, Inc. *
7* All rights reserved. *
8* *
9* This material resulted from work developed under a Government Contract and *
10* is subject to the following license: The Government retains a paid-up, *
11* nonexclusive, irrevocable worldwide license to reproduce, prepare derivative *
12* works, perform publicly and display publicly by or for the Government, *
13* including the right to distribute to other Government contractors. Neither *
14* the United States nor the United States Department of Energy, nor any of *
15* their employees, makes any warranty, express or implied, or assumes any *
16* legal liability or responsibility for the accuracy, completeness, or *
17* usefulness of any information, apparatus, product, or process disclosed, or *
18* represents that its use would not infringe privately owned rights. *
19* *
20* *
21* Written by Paul Lebrun *
22* *
23* *
24*******************************************************************************/
25#include <stdio.h>
26#include <string.h>
27#include <sys/param.h>
28#include <rpc/types.h>
29#include <sys/types.h>
30#include <rpc/xdr.h>
31#include <limits.h>
32#include <stdlib.h>
33#include <time.h>
34#ifdef SUNOS
35#include <floatingpoint.h>
36#else /* SUNOS */
37#include <float.h>
38#endif /* SUNOS */
39#include "mcf_nTupleDescript.h"
40#include "mcf_xdr.h"
41#include "mcf_xdr_Ntuple.h"
42#include "mcfio_Dict.h"
43#include "mcfio_Util1.h"
44#include "mcf_NTuIOUtils.h"
45#include "mcfio_Direct.h"
46#include "mcfio_Block.h"
47#ifndef FALSE
48#define FALSE 0
49#endif
50#ifndef TRUE
51#define TRUE 1
52#endif
53
54int mcfioC_Block(int stream, int blkid,
55 bool_t xdr_filtercode(XDR *xdrs, int *blockid, int *ntot, char **version))
56/*
57** Routine to decode or encode a particular Block. Return 1 if O.K,
58** -1 if a problem or unknow block.
59**
60** Adding Ntuple instances ... October 1995.
61*/
62{
63 int i, j, jstr, idtmp, ntot, nbuff;
64 bool_t ok;
65 u_int p1;
66 mcfStream *str;
67
68 if (McfStreamPtrList == NULL) {
69 fprintf(stderr,
70 " mcfio_Block: You must first initialize by calling mcfio_Init.\n");
71 return -1;
72 }
73 jstr = stream-1;
74 if (McfStreamPtrList[jstr] == NULL) {
75 fprintf(stderr,
76 " mcfio_Block: First, declare the stream by calling mcfio_Open...\n");
77 return -1;
78 }
79 str = McfStreamPtrList[jstr];
80 if ((str->row == MCFIO_WRITE) &&
81 (str->fhead->nBlocks == str->ehead->nBlocks)) {
82 fprintf(stderr,
83 " mcfio_Block: Maximum number of Blocks reached for stream %d ...\n", stream);
84 fprintf(stderr,
85 " Please upgrade the declaration mcfio_Open statement \n");
86 return -1;
87 }
88
89 if (str->row == MCFIO_READ) {
90 for(i=0, j=-1; i<str->ehead->nBlocks; i++) {
91 if (str->ehead->blockIds[i] == blkid) j = i;
92 }
93 if (j == -1) {
94/*
95 fprintf(stderr,
96 " mcfio_Block: Unable to find block i.d. %d in Stream %d \n", blkid, stream);
97*/
98 return -1;
99 }
100 if (xdr_setpos(str->xdr,str->ehead->ptrBlocks[j]) == FALSE) {
101 fprintf(stderr,
102 " mcfio_Block: Unable to position stream at block %d \n", blkid);
103 return -1;
104 }
105 str->currentPos = str->ehead->ptrBlocks[j];
106 } else if (str->row == MCFIO_WRITE) {
107 idtmp = blkid;
108 /*
109 ** if to Sequential media, one first has to make sure we have
110 ** enough room in the buffer.
111 */
112 if (str->dos == MCFIO_SEQUENTIAL) {
113 str->xdr->x_op = XDR_MCFIOCODE;
114 ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
115 str->xdr->x_op = XDR_ENCODE;
116 if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
117 /*
118 ** Once again, I don't trust realloc, got to copy to the second
119 ** buffer.
120 */
121 nbuff = 1 +
122 (((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
123 str->maxlrec);
124 str->buffer2 =
125 (char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
126 memcpy(str->buffer2, str->buffer,
127 (str->currentPos - str->firstPos));
128 free(str->buffer);
129 str->buffer = str->buffer2;
130 str->buffer2 = NULL;
131 str->bufferSize = str->maxlrec * nbuff;
132 xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
133 if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
134 fprintf(stderr,
135 " mcfio_Block:\n\
136 Unable to position stream %d at block %d after realocation.\n", stream, blkid);
137 return -1;
138 }
139 }
140 }
141 }
142 p1 = str->currentPos;
143 ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
144 if (ok == FALSE) {
145 fprintf(stderr,
146 " mcfio_Block: Unable to encode or decode block I.D. %d \n", blkid);
147 j = str->ehead->nBlocks;
148 if (xdr_setpos(str->xdr,p1) == FALSE)
149 fprintf(stderr,
150 " mcfio_Block: Unable to position stream at block %d \n", blkid);
151 return -1;
152 }
153 if(blkid != idtmp) {
154 fprintf(stderr,
155 " mcfio_Block: Unexpected I.D = %d found instead of I.D. %d \n",
156 idtmp, blkid);
157 return -1;
158 }
159 if (str->row == MCFIO_WRITE) {
160 str->ehead->blockIds[str->ehead->nBlocks] = blkid;
161 str->ehead->ptrBlocks[str->ehead->nBlocks] = p1;
162 str->ehead->nBlocks++;
163 }
164 str->currentPos = xdr_getpos(str->xdr);
165 str->numWordsC += (ntot/4);
166 str->numWordsT += ((str->currentPos-p1)/4);
167 return 1;
168
169}
170int mcfioC_NTuple(int stream, int nTupleId, char * version)
171{
172 int i, j, jstr, ntot, nbuff;
173 bool_t ok;
174 u_int p1;
175 mcfStream *str;
176 nTuDDL *ddl;
177 descrGenNtuple *dNTu;
178
179 if (McfStreamPtrList == NULL) {
180 fprintf(stderr,
181 " mcfio_NTuple: You must first initialize by calling mcfio_Init.\n");
182 return -1;
183 }
184 jstr = stream-1;
185 if (McfStreamPtrList[jstr] == NULL) {
186 fprintf(stderr,
187 " mcfio_NTuple: First, declare the stream by calling mcfio_Open...\n");
188 return -1;
189 }
190
191 ddl = mcf_GetNTuByStreamID(stream, nTupleId);
192 if (ddl == NULL) {
193 fprintf(stderr,
194 " mcfio_NTuple: Illegal or inexistant NTuple Id %d for stream %d \n",
195 nTupleId, stream);
196 return -1;
197 }
198 if (ddl->reference == NULL) dNTu = ddl->descrNtu;
199 else dNTu = ddl->reference->descrNtu;
200 str = McfStreamPtrList[jstr];
201 if ((str->row == MCFIO_WRITE) &&
202 (str->fhead->nNTuples == str->ehead->nNTuples)) {
203 fprintf(stderr,
204" mcfio_NTuple: Maximum number of NTuples reached for stream %d ...\n", stream);
205 fprintf(stderr,
206 " Please upgrade the Ntuple declarations statements. \n");
207 return -1;
208 }
209
210 if (str->row == MCFIO_READ) {
211 for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
212 if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
213 }
214 if (j == -1) {
215 fprintf(stderr,
216 " mcfio_NTuple: Unable to find NTuple i.d. %d in Stream %d \n",
217 nTupleId, stream);
218 return -1;
219 }
220 if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
221 fprintf(stderr,
222 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
223 return -1;
224 }
225 str->currentPos = str->ehead->ptrNTuples[j];
226 } else if (str->row == MCFIO_WRITE) {
227 /*
228 ** if to Sequential media, one first has to make sure we have
229 ** enough room in the buffer.
230 */
231 if (str->dos == MCFIO_SEQUENTIAL) {
232 str->xdr->x_op = XDR_MCFIOCODE;
233 ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot,
234 ddl->seqNTuId, version);
235 str->xdr->x_op = XDR_ENCODE;
236 if (ok == FALSE) {
237 fprintf(stderr,
238 "mcfio_NTuple: can not Encode or Decode Ntuple id %d on Seq. Stream %d ",
239 nTupleId, stream);
240 return -1;
241 }
242 if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
243 /*
244 ** Once again, I don't trust realloc, got to copy to the second
245 ** buffer.
246 */
247 nbuff = 1 +
248 (((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
249 str->maxlrec);
250 str->buffer2 =
251 (char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
252 memcpy(str->buffer2, str->buffer,
253 (str->currentPos - str->firstPos));
254 free(str->buffer);
255 str->buffer = str->buffer2;
256 str->buffer2 = NULL;
257 str->bufferSize = str->maxlrec * nbuff;
258 xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
259 if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
260 fprintf(stderr,
261 " mcfio_NTuple:\n\
262 Unable to position stream %d at Ntuple %d after realocation.\n",
263 stream, nTupleId);
264 return -1;
265 }
266 }
267 }
268 }
269 p1 = str->currentPos;
270 ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot, ddl->seqNTuId, version);
271 if (ok == FALSE) {
272 fprintf(stderr,
273 " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
274 nTupleId);
275 j = str->ehead->nNTuples;
276 if (xdr_setpos(str->xdr,p1) == FALSE)
277 fprintf(stderr,
278 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
279 return -1;
280 }
281 if (str->row == MCFIO_WRITE) {
282 str->ehead->nTupleIds[str->ehead->nNTuples] = ddl->seqNTuId;
283 str->ehead->ptrNTuples[str->ehead->nNTuples] = p1;
284 str->ehead->nNTuples++;
285 }
286 str->currentPos = xdr_getpos(str->xdr);
287 str->numWordsC += (ntot/4);
288 str->numWordsT += ((str->currentPos-p1)/4);
289 return 1;
290
291}
292/*
293** Optimized version used exclusively to read the multiplicity value
294** within an NTuple. It is assumed that the stream is open read direct
295** access (No checks!), and the event table is available, and the
296** NTuple is accessible. Once again, No checks! Use at your onw risk.
297** Also, we do not keep record of the number of byte Read.
298*/
299int mcfioC_NTupleMult(int stream, int nTupleId, char * version)
300{
301 int i, j, jstr, ntot;
302 bool_t ok;
303 mcfStream *str;
304 nTuDDL *ddl;
305 descrGenNtuple *dNTu;
306
307 jstr = stream-1;
308 ddl = mcf_GetNTuByStreamID(stream, nTupleId);
309 if (ddl->reference == NULL) dNTu = ddl->descrNtu;
310 else dNTu = ddl->reference->descrNtu;
311 str = McfStreamPtrList[jstr];
312 for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
313 if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
314 }
315 if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
316 fprintf(stderr,
317 " mcfio_NTupleMult: Unable to position stream at NTuple %d \n", nTupleId);
318 return -1;
319 }
320 str->currentPos = str->ehead->ptrNTuples[j];
321 if (dNTu->multXDROffset == 0)
322 ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
323 ddl->seqNTuId, version);
324 else ok = xdr_mcfast_NTupleMult(str, dNTu, version);
325 if (ok == FALSE) {
326 fprintf(stderr,
327 " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
328 nTupleId);
329 j = str->ehead->nNTuples;
330 if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
331 fprintf(stderr,
332 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
333 return -1;
334 }
335 /*
336 ** This probably could be optimized away. Note the that the current
337 ** position of the stream strored in str->currentPos is no longer
338 ** valied exiting this routine. However, there is enough redundancy
339 ** in the data structure to figure out where we could go..
340 */
341 /* xdr_setpos(str->xdr, str->currentPos); */
342 return TRUE;
343
344}
345
346/*
347** Optimized version used exclusively to read a specific variable
348** within an NTuple. Valid only if the variable is of fixed size
349** (e.g. not indexed by multiplicity) or if the data structure organization is
350** of type parallel array. It is assumed that the stream is open read direct
351** access (No checks!), and the event table is available, and the
352** NTuple is accessible. Once again, No checks! Use at your own risk.
353*/
354int mcfioC_NTupleVar(int stream, int nTupleId, int ivar, char * version)
355{
356 int i, j, jstr, ntot;
357 bool_t ok;
358 mcfStream *str;
359 nTuDDL *ddl;
360 descrGenNtuple *dNTu;
361
362 jstr = stream-1;
363 ddl = mcf_GetNTuByStreamID(stream, nTupleId);
364 if (ddl->reference == NULL) dNTu = ddl->descrNtu;
365 else dNTu = ddl->reference->descrNtu;
366 str = McfStreamPtrList[jstr];
367 for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
368 if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
369 }
370 if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
371 fprintf(stderr,
372 " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
373 return -1;
374 }
375 str->currentPos = str->ehead->ptrNTuples[j];
376 if (dNTu->multXDROffset == 0)
377 ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
378 ddl->seqNTuId, version);
379 else ok = xdr_mcfast_NTupleVar(str, dNTu, ivar, version);
380 if (ok == FALSE) {
381 fprintf(stderr,
382 " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
383 nTupleId);
384 j = str->ehead->nNTuples;
385 if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
386 fprintf(stderr,
387 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
388 return -1;
389 }
390 return TRUE;
391
392}
393/*
394** Optimized version used exclusively to read a specific variable within a
395** substructure within an NTuple. Valid only if of type indexed
396** and if the data structure organization is
397** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct
398** access (No checks!), and the event table is available, and the
399** NTuple is accessible. Once again, No checks! Use at your own risk.
400*/
401int mcfioC_NTupleSubVar(int stream, int nTupleId, int ivar, int multIndex,
402 char * version)
403{
404 int i, j, jstr, ntot;
405 bool_t ok;
406 mcfStream *str;
407 nTuDDL *ddl;
408 descrGenNtuple *dNTu;
409
410 jstr = stream-1;
411 ddl = mcf_GetNTuByStreamID(stream, nTupleId);
412 if (ddl->reference == NULL) dNTu = ddl->descrNtu;
413 else dNTu = ddl->reference->descrNtu;
414 str = McfStreamPtrList[jstr];
415 for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
416 if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
417 }
418 if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
419 fprintf(stderr,
420 " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
421 return -1;
422 }
423 str->currentPos = str->ehead->ptrNTuples[j];
424 if (dNTu->multXDROffset == 0)
425 ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
426 ddl->seqNTuId, version);
427 else ok = xdr_mcfast_NTupleSubVar(str, dNTu, ivar, multIndex, version);
428 if (ok == FALSE) {
429 fprintf(stderr,
430 " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
431 nTupleId);
432 j = str->ehead->nNTuples;
433 if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
434 fprintf(stderr,
435 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
436 return -1;
437 }
438 return TRUE;
439
440}
441/*
442** Optimized version used exclusively to read a specific
443** substructure within an NTuple. Valid only if of type indexed
444** and if the data structure organization is
445** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct
446** access (No checks!), and the event table is available, and the
447** NTuple is accessible. Once again, No checks! Use at your own risk.
448*/
449int mcfioC_NTupleSubStruct(int stream, int nTupleId, int multIndex,
450 char * version)
451{
452 int i, j, jstr, ntot;
453 bool_t ok;
454 mcfStream *str;
455 nTuDDL *ddl;
456 descrGenNtuple *dNTu;
457
458 jstr = stream-1;
459 ddl = mcf_GetNTuByStreamID(stream, nTupleId);
460 if (ddl->reference == NULL) dNTu = ddl->descrNtu;
461 else dNTu = ddl->reference->descrNtu;
462 str = McfStreamPtrList[jstr];
463 for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
464 if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
465 }
466 if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
467 fprintf(stderr,
468 " mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
469 return -1;
470 }
471 str->currentPos = str->ehead->ptrNTuples[j];
472 if (dNTu->multXDROffset == 0)
473 ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
474 ddl->seqNTuId, version);
475 else ok = xdr_mcfast_NTupleSubStruct(str, dNTu, multIndex, version);
476 if (ok == FALSE) {
477 fprintf(stderr,
478 " mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
479 nTupleId);
480 j = str->ehead->nNTuples;
481 if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
482 fprintf(stderr,
483 " mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
484 return -1;
485 }
486 return TRUE;
487
488}
Note: See TracBrowser for help on using the repository browser.