1
/*******************************************************************************
3
* mcfio_Block.c -- Utility routines for the McFast Monte-Carlo *
4
* The routine to encode/decode a block *
6
* Copyright (c) 1994 Universities Research Association, Inc. *
7
* All rights reserved. *
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. *
21
* Written by Paul Lebrun *
24
*******************************************************************************/
27
#include <sys/param.h>
28
#include <rpc/types.h>
29
#include <sys/types.h>
35
#include <floatingpoint.h>
39
#include "mcf_nTupleDescript.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"
54
int mcfioC_Block(int stream, int blkid,
55
bool_t xdr_filtercode(XDR *xdrs, int *blockid, int *ntot, char **version))
57
** Routine to decode or encode a particular Block. Return 1 if O.K,
58
** -1 if a problem or unknow block.
60
** Adding Ntuple instances ... October 1995.
63
int i, j, jstr, idtmp, ntot, nbuff;
68
if (McfStreamPtrList == NULL) {
70
" mcfio_Block: You must first initialize by calling mcfio_Init.\n");
74
if (McfStreamPtrList[jstr] == NULL) {
76
" mcfio_Block: First, declare the stream by calling mcfio_Open...\n");
79
str = McfStreamPtrList[jstr];
80
if ((str->row == MCFIO_WRITE) &&
81
(str->fhead->nBlocks == str->ehead->nBlocks)) {
83
" mcfio_Block: Maximum number of Blocks reached for stream %d ...\n", stream);
85
" Please upgrade the declaration mcfio_Open statement \n");
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;
95
" mcfio_Block: Unable to find block i.d. %d in Stream %d \n", blkid, stream);
98
if (xdr_setpos(str->xdr,str->ehead->ptrBlocks[j]) == FALSE) {
100
" mcfio_Block: Unable to position stream at block %d \n", blkid);
103
str->currentPos = str->ehead->ptrBlocks[j];
104
} else if (str->row == MCFIO_WRITE) {
107
** if to Sequential media, one first has to make sure we have
108
** enough room in the buffer.
110
if (str->dos == MCFIO_SEQUENTIAL) {
111
str->xdr->x_op = XDR_MCFIOCODE;
112
ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
113
str->xdr->x_op = XDR_ENCODE;
114
if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
116
** Once again, I don't trust realloc, got to copy to the second
120
(((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
123
(char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
124
memcpy(str->buffer2, str->buffer,
125
(str->currentPos - str->firstPos));
127
str->buffer = str->buffer2;
129
str->bufferSize = str->maxlrec * nbuff;
130
xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
131
if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
134
Unable to position stream %d at block %d after realocation.\n", stream, blkid);
140
p1 = str->currentPos;
141
ok = xdr_filtercode(str->xdr, &idtmp, &ntot, McfGenericVersion);
144
" mcfio_Block: Unable to encode or decode block I.D. %d \n", blkid);
145
j = str->ehead->nBlocks;
146
if (xdr_setpos(str->xdr,p1) == FALSE)
148
" mcfio_Block: Unable to position stream at block %d \n", blkid);
153
" mcfio_Block: Unexpected I.D = %d found instead of I.D. %d \n",
157
if (str->row == MCFIO_WRITE) {
158
str->ehead->blockIds[str->ehead->nBlocks] = blkid;
159
str->ehead->ptrBlocks[str->ehead->nBlocks] = p1;
160
str->ehead->nBlocks++;
162
str->currentPos = xdr_getpos(str->xdr);
163
str->numWordsC += (ntot/4);
164
str->numWordsT += ((str->currentPos-p1)/4);
168
int mcfioC_NTuple(int stream, int nTupleId, char * version)
170
int i, j, jstr, idtmp, ntot, nbuff;
175
descrGenNtuple *dNTu;
177
if (McfStreamPtrList == NULL) {
179
" mcfio_NTuple: You must first initialize by calling mcfio_Init.\n");
183
if (McfStreamPtrList[jstr] == NULL) {
185
" mcfio_NTuple: First, declare the stream by calling mcfio_Open...\n");
189
ddl = mcf_GetNTuByStreamID(stream, nTupleId);
192
" mcfio_NTuple: Illegal or inexistant NTuple Id %d for stream %d \n",
196
if (ddl->reference == NULL) dNTu = ddl->descrNtu;
197
else dNTu = ddl->reference->descrNtu;
198
str = McfStreamPtrList[jstr];
199
if ((str->row == MCFIO_WRITE) &&
200
(str->fhead->nNTuples == str->ehead->nNTuples)) {
202
" mcfio_NTuple: Maximum number of NTuples reached for stream %d ...\n", stream);
204
" Please upgrade the Ntuple declarations statements. \n");
208
if (str->row == MCFIO_READ) {
209
for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
210
if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
214
" mcfio_NTuple: Unable to find NTuple i.d. %d in Stream %d \n",
218
if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
220
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
223
str->currentPos = str->ehead->ptrNTuples[j];
224
} else if (str->row == MCFIO_WRITE) {
226
** if to Sequential media, one first has to make sure we have
227
** enough room in the buffer.
229
if (str->dos == MCFIO_SEQUENTIAL) {
230
str->xdr->x_op = XDR_MCFIOCODE;
231
ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot,
232
ddl->seqNTuId, version);
233
str->xdr->x_op = XDR_ENCODE;
236
"mcfio_NTuple: can not Encode or Decode Ntuple id % on Seq. Stream %d ",
240
if ((str->currentPos + 4*(ntot + 1)) > str->bufferSize) {
242
** Once again, I don't trust realloc, got to copy to the second
246
(((4*(ntot + 1)) + (str->currentPos - str->firstPos))/
249
(char *) malloc (sizeof(char) * (str->maxlrec *nbuff));
250
memcpy(str->buffer2, str->buffer,
251
(str->currentPos - str->firstPos));
253
str->buffer = str->buffer2;
255
str->bufferSize = str->maxlrec * nbuff;
256
xdrmem_create(str->xdr, str->buffer, str->bufferSize, XDR_ENCODE);
257
if (xdr_setpos(str->xdr, str->currentPos) == FALSE) {
260
Unable to position stream %d at Ntuple %d after realocation.\n",
267
p1 = str->currentPos;
268
ok = xdr_mcfast_NTuple(str->xdr, dNTu, &ntot, ddl->seqNTuId, version);
271
" mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
273
j = str->ehead->nNTuples;
274
if (xdr_setpos(str->xdr,p1) == FALSE)
276
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
279
if (str->row == MCFIO_WRITE) {
280
str->ehead->nTupleIds[str->ehead->nNTuples] = ddl->seqNTuId;
281
str->ehead->ptrNTuples[str->ehead->nNTuples] = p1;
282
str->ehead->nNTuples++;
284
str->currentPos = xdr_getpos(str->xdr);
285
str->numWordsC += (ntot/4);
286
str->numWordsT += ((str->currentPos-p1)/4);
291
** Optimized version used exclusively to read the multiplicity value
292
** within an NTuple. It is assumed that the stream is open read direct
293
** access (No checks!), and the event table is available, and the
294
** NTuple is accessible. Once again, No checks! Use at your onw risk.
295
** Also, we do not keep record of the number of byte Read.
297
int mcfioC_NTupleMult(int stream, int nTupleId, char * version)
299
int i, j, jstr, idtmp, ntot, nbuff;
303
descrGenNtuple *dNTu;
306
ddl = mcf_GetNTuByStreamID(stream, nTupleId);
307
if (ddl->reference == NULL) dNTu = ddl->descrNtu;
308
else dNTu = ddl->reference->descrNtu;
309
str = McfStreamPtrList[jstr];
310
for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
311
if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
313
if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
315
" mcfio_NTupleMult: Unable to position stream at NTuple %d \n", nTupleId);
318
str->currentPos = str->ehead->ptrNTuples[j];
319
if (dNTu->multXDROffset == 0)
320
ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
321
ddl->seqNTuId, version);
322
else ok = xdr_mcfast_NTupleMult(str, dNTu, version);
325
" mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
327
j = str->ehead->nNTuples;
328
if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
330
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
334
** This probably could be optimized away. Note the that the current
335
** position of the stream strored in str->currentPos is no longer
336
** valied exiting this routine. However, there is enough redundancy
337
** in the data structure to figure out where we could go..
339
/* xdr_setpos(str->xdr, str->currentPos); */
345
** Optimized version used exclusively to read a specific variable
346
** within an NTuple. Valid only if the variable is of fixed size
347
** (e.g. not indexed by multiplicity) or if the data structure organization is
348
** of type parallel array. It is assumed that the stream is open read direct
349
** access (No checks!), and the event table is available, and the
350
** NTuple is accessible. Once again, No checks! Use at your own risk.
352
int mcfioC_NTupleVar(int stream, int nTupleId, int ivar, char * version)
354
int i, j, jstr, idtmp, ntot, nbuff;
358
descrGenNtuple *dNTu;
361
ddl = mcf_GetNTuByStreamID(stream, nTupleId);
362
if (ddl->reference == NULL) dNTu = ddl->descrNtu;
363
else dNTu = ddl->reference->descrNtu;
364
str = McfStreamPtrList[jstr];
365
for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
366
if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
368
if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
370
" mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
373
str->currentPos = str->ehead->ptrNTuples[j];
374
if (dNTu->multXDROffset == 0)
375
ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
376
ddl->seqNTuId, version);
377
else ok = xdr_mcfast_NTupleVar(str, dNTu, ivar, version);
380
" mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
382
j = str->ehead->nNTuples;
383
if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
385
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
392
** Optimized version used exclusively to read a specific variable within a
393
** substructure within an NTuple. Valid only if of type indexed
394
** and if the data structure organization is
395
** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct
396
** access (No checks!), and the event table is available, and the
397
** NTuple is accessible. Once again, No checks! Use at your own risk.
399
int mcfioC_NTupleSubVar(int stream, int nTupleId, int ivar, int multIndex,
402
int i, j, jstr, idtmp, ntot, nbuff;
406
descrGenNtuple *dNTu;
409
ddl = mcf_GetNTuByStreamID(stream, nTupleId);
410
if (ddl->reference == NULL) dNTu = ddl->descrNtu;
411
else dNTu = ddl->reference->descrNtu;
412
str = McfStreamPtrList[jstr];
413
for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
414
if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
416
if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
418
" mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
421
str->currentPos = str->ehead->ptrNTuples[j];
422
if (dNTu->multXDROffset == 0)
423
ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
424
ddl->seqNTuId, version);
425
else ok = xdr_mcfast_NTupleSubVar(str, dNTu, ivar, multIndex, version);
428
" mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
430
j = str->ehead->nNTuples;
431
if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
433
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);
440
** Optimized version used exclusively to read a specific
441
** substructure within an NTuple. Valid only if of type indexed
442
** and if the data structure organization is
443
** of type VAX FORTRAN d/s. It is assumed that the stream is open read direct
444
** access (No checks!), and the event table is available, and the
445
** NTuple is accessible. Once again, No checks! Use at your own risk.
447
int mcfioC_NTupleSubStruct(int stream, int nTupleId, int multIndex,
450
int i, j, jstr, idtmp, ntot, nbuff;
454
descrGenNtuple *dNTu;
457
ddl = mcf_GetNTuByStreamID(stream, nTupleId);
458
if (ddl->reference == NULL) dNTu = ddl->descrNtu;
459
else dNTu = ddl->reference->descrNtu;
460
str = McfStreamPtrList[jstr];
461
for(i=0, j=-1; i<str->ehead->nNTuples; i++) {
462
if (str->ehead->nTupleIds[i] == ddl->seqNTuId) j = i;
464
if (xdr_setpos(str->xdr,str->ehead->ptrNTuples[j]) == FALSE) {
466
" mcfio_NTupleVar: Unable to position stream at NTuple %d \n", nTupleId);
469
str->currentPos = str->ehead->ptrNTuples[j];
470
if (dNTu->multXDROffset == 0)
471
ok = xdr_mcfast_NTupleXDRPtr(str->xdr, dNTu, &ntot,
472
ddl->seqNTuId, version);
473
else ok = xdr_mcfast_NTupleSubStruct(str, dNTu, multIndex, version);
476
" mcfio_NTuple: Unable to encode or decode NTuple I.D. %d \n",
478
j = str->ehead->nNTuples;
479
if (xdr_setpos(str->xdr, str->currentPos) == FALSE)
481
" mcfio_NTuple: Unable to position stream at NTuple %d \n", nTupleId);