File: bcast.c

package info (click to toggle)
yorick 2.2.03%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,620 kB
  • ctags: 9,317
  • sloc: ansic: 85,521; sh: 1,665; cpp: 1,282; lisp: 1,234; makefile: 1,034; fortran: 19
file content (480 lines) | stat: -rw-r--r-- 14,067 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * $Id: bcast.c,v 1.1 2005-09-18 22:03:48 dhmunro Exp $
 * Implement generic broadcast, scatter, and gather functions.
 */
/* Copyright (c) 2005, The Regents of the University of California.
 * All rights reserved.
 * This file is part of yorick (http://yorick.sourceforge.net).
 * Read the accompanying LICENSE file for details.
 */

#include "bcast.h"

static char *daddr, *saddr;
static StructDef *copyBase;
static long size;
static Copier *Copy;

static void BlockScat(const Strider *strider);
static void BlockGath(const Strider *strider);

extern void YErrorIO(const char *msg);

/*--------------------------------------------------------------------------*/
/* Broadcast function is only relevant to Yorick, not binary file package.  */
#ifndef NOT_YORICK

/* Broadcast(dst, ddims, src, sdims, base)
   broadcasts the src array into the dst array (where both src
   and dst array elements are size bytes long), returning 0 on
   success, 1 if sdims is not L-conformable with ddims. */

/* Currently, FastCopier is selected by base->dataOps->promoteID, which
   requires knowledge of Yorick's Operations struct.  This only affects
   the Broadcast operation, and only Scatter and Gather are relevant to
   the binary file package.  */
static int BlockCast(const Dimension *ddims, const Dimension *sdims,
                     long nCopies);

/* Without special routines to do scalar broadcasts, Yorick bogs down
   unacceptably doing the memcpy called from CopyX.  */
typedef void FastCopier(long, long);
static FastCopier *FastCopy;
static FastCopier FastCCopy, FastSCopy, FastICopy, FastLCopy,
  FastFCopy, FastDCopy, FastZCopy, FastXCopy;
static FastCopier *FCopiers[]= { &FastCCopy, &FastSCopy, &FastICopy,
              &FastLCopy, &FastFCopy, &FastDCopy, &FastZCopy, &FastXCopy };

#undef OPERATION
#define OPERATION(opname, typeD) \
static void opname(long n, long nCopies) \
{ typeD *dst= (typeD *)daddr,  *src= (typeD *)saddr; \
  long i, j, jx= n*nCopies;  register typeD src0; \
  if (nCopies>n) { for (i=0 ; i<n ; i++) { \
      src0= src[i];  for (j=i ; j<jx ; j+=n) dst[j]= src0; } \
    } else { for (j=0 ; j<jx ; j+=n) \
      for (i=0 ; i<n ; i++) dst[i+j]= src[i]; } \
  daddr= (char *)(dst+jx);  saddr= (char *)(src+n); \
}

OPERATION(FastCCopy, char)
OPERATION(FastSCopy, short)
OPERATION(FastICopy, int)
OPERATION(FastLCopy, long)
OPERATION(FastFCopy, float)
OPERATION(FastDCopy, double)

static void FastZCopy(long n, long nCopies)
{
  double *dst= (double *)daddr;
  double *src= (double *)saddr;
  long i, j, jx= (n*=2)*nCopies;
  register double src0, src1;
  if (nCopies>n) {
    for (i=0 ; i<n ; i+=2) {
      src0= src[i];  src1= src[i+1];
      for (j=i ; j<jx ; j+=n) { dst[j]= src0; dst[j+1]= src1; }
    }
  } else {
    for (j=0 ; j<jx ; j+=n)
      for (i=0 ; i<n ; i+=2) { dst[i+j]= src[i]; dst[i+j+1]= src[i+1]; }
  }
  daddr= (char *)(dst+jx);
  saddr= (char *)(src+n);
}

/* No help for the general case--this is how I originally programmed it.  */
static void FastXCopy(long n, long nCopies)
{
  long nbytes= n*size;
  while (nCopies--) {
    Copy(copyBase, daddr, saddr, n);
    daddr+= nbytes;
  }
  saddr+= nbytes;
}

static int BlockCast(const Dimension *ddims, const Dimension *sdims,
                     long nCopies)
{
  long n= 1;

  /* skip past leading source 1's, which are just more copies */
  while (sdims && sdims->number==1) {
    nCopies*= ddims->number;
    sdims= sdims->next;
    ddims= ddims->next;
  }

  /* as long as sdims and dims agree, a block copy is possible */
  while (sdims && sdims->number==ddims->number) {
    n*= ddims->number;
    ddims= ddims->next;
    sdims= sdims->next;
  }

  if (!sdims) {
    /* if the dimension lists are exhausted, just make nCopies of
       blocks of n copyBase StructDefs */
    FastCopy(n, nCopies);

  } else if (sdims->number==1) {
    /* must recurse to do a sparse copy */
    char *saddr0= saddr;
    long i;
    for (;;) {
      for (i=0 ; i<n ; i++) BlockCast(ddims, sdims, 1L);
      if (! --nCopies) break;
      saddr= saddr0;
    }

  } else {
    /* sdims->number!=ddims->number and sdims->number!=1 is an error */
    return 1;
  }
  return 0;
}

int Broadcast(void *dst, const Dimension *ddims,
              void *src, const Dimension *sdims, StructDef *base)
{
  int n= CountDims(ddims)-CountDims(sdims);
  long nCopies= 1;
  saddr= src;
  daddr= dst;
  FastCopy= FCopiers[base->dataOps->promoteID];
  if (FastCopy==&FastXCopy) {
    copyBase= base;
    size= base->size;
    Copy= base->Copy;
  }
  if (n<0) {
    do {
      if (sdims->number > 1) return 1;
      sdims= sdims->next;
    } while (++n);
  } else if (n>0) {
    do {
      nCopies*= ddims->number;
      ddims= ddims->next;
    } while (--n);
  }
  return BlockCast(ddims, sdims, nCopies);
}

#endif
/* Scatter and Gather functions relevant even if NOT_YORICK.  */
/*--------------------------------------------------------------------------*/

/* Scatter(dst, strider, src, base, number)
   scatters the src array into the dst array, given the strider for
   the dst array and the base StructDef for the objects to be scattered.
   If the strider is 0, the given number of contiguous objects is
   copied.
      x(index list)= expression      */

/* Without special routines for gather and scatter, Yorick bogs down
   unacceptably of some platforms (e.g.- Crays).  */
typedef void FastScatter(long, long, long*);
static FastScatter *FastScat;
static FastScatter FastCScat, FastSScat, FastIScat, FastLScat,
  FastFScat, FastDScat;
static FastScatter *FScatters[]= { &FastCScat, &FastSScat, &FastIScat,
              &FastLScat, &FastFScat, &FastDScat, 0, 0 };

#undef OPERATION
#define OPERATION(opname, typeD) \
static void opname(long stride, long n, long *list) \
{ \
  typeD *dst= (typeD *)daddr,  *src= (typeD *)saddr; \
  if (stride!=size) { \
    if (!list) for (;n--;src++,daddr+=stride) *((typeD*)daddr)= src[0]; \
    else for (;n--;src++,list++) *((typeD*)(daddr+list[0]*stride))= src[0]; \
  } else { \
    if (!list) for ( ; n-- ; src++,dst++) dst[0]= src[0]; \
    else for (; n-- ; src++,list++) dst[list[0]]= src[0]; \
  } \
  saddr= (char *)src; \
}

OPERATION(FastCScat, char)
OPERATION(FastSScat, short)
OPERATION(FastIScat, int)
OPERATION(FastLScat, long)
OPERATION(FastFScat, float)
OPERATION(FastDScat, double)

static void BlockScat(const Strider *strider)
{
  Strider *next= strider->next;
  long stride= strider->stride;
  long i, n= strider->number;
  Array *indexList= strider->indexList;

  char *daddr0= daddr;

  if (!indexList) {
    if (next) {
      for (i=0 ; i<n ; i++) {
        BlockScat(next);
        daddr+= stride;
        /* saddr already incremented by other branches of BlockScat */
      }
    } else if (FastScat) {
      FastScat(stride, n, (long *)0);
    } else if (stride!=size) {
      for (i=0 ; i<n ; i++) {
        Copy(copyBase, daddr, saddr, 1L);
        daddr+= stride;
        saddr+= size;
      }
    } else {
      Copy(copyBase, daddr, saddr, n);
      saddr+= n*size;
    }

  } else {
    long *list= indexList->value.l;
    if (next) {
      for (i=0 ; i<n ; i++) {
        daddr= daddr0+stride*list[i];
        BlockScat(next);
        /* saddr already incremented by other branches of BlockScat */
      }
    } else if (FastScat) {
      FastScat(stride, n, list);
    } else {
      if (size==stride) {
        long j, li, ni;
        for (i=0 ; i<n ; i++) {
          ni= 1;  /* "where" function often leaves sequential lists */
          li= list[i];
          for (j=li+1 ; i+1<n && j==list[i+1] ; i++, j++) ni++;
          Copy(copyBase, daddr+stride*li, saddr, ni);
          saddr+= size*ni;
        }
      } else {
        for (i=0 ; i<n ; i++) {
          Copy(copyBase, daddr+stride*list[i], saddr, 1L);
          saddr+= size;
        }
      }
    }
  }

  /* Always leave daddr as we found it for proper recursion. */
  daddr= daddr0;
}

void Scatter(void *src, void *dst, StructDef *base, long number,
             const Strider *strider)
{
  if (strider) {
    saddr= src;
    daddr= dst;
    FastScat= FScatters[base->dataOps->promoteID];
    if (!FastScat) {
      copyBase= base;
      Copy= base->Copy;
    }
    size= base->size;
    BlockScat(strider);
  } else {
    /* 0 Strider* uses the specified number instead */
    base->Copy(base, dst, src, number);
  }
}

/*--------------------------------------------------------------------------*/

/* Gather(dst, src, strider, base, number)
   gathers into the dst array from the src array, given the strider for
   the src array and the base StructDef for the objects to be gathered.
   If the strider is 0, the given number of contiguous objects is
   copied.
      ... evaluate x(index list) ...  */

/* Without special routines for gather and scatter, Yorick bogs down
   unacceptably of some platforms (e.g.- Crays).  */
typedef void FastGather(long, long, long*);
static FastGather *FastGath;
static FastGather FastCGath, FastSGath, FastIGath, FastLGath,
  FastFGath, FastDGath;
static FastGather *FGathers[]= { &FastCGath, &FastSGath, &FastIGath,
              &FastLGath, &FastFGath, &FastDGath, 0, 0 };

#undef OPERATION
#define OPERATION(opname, typeD) \
static void opname(long stride, long n, long *list) \
{ \
  typeD *dst= (typeD *)daddr,  *src= (typeD *)saddr; \
  if (stride!=size) { \
    if (!list) for (;n--;dst++,saddr+=stride) dst[0]= *((typeD*)saddr); \
    else for (;n--;dst++,list++) dst[0]= *((typeD*)(saddr+list[0]*stride)); \
  } else { \
    if (!list) for (; n-- ; dst++,src++) dst[0]= src[0]; \
    else for (; n-- ; dst++,list++) dst[0]= src[list[0]]; \
  } \
  daddr= (char *)dst; \
}

OPERATION(FastCGath, char)
OPERATION(FastSGath, short)
OPERATION(FastIGath, int)
OPERATION(FastLGath, long)
OPERATION(FastFGath, float)
OPERATION(FastDGath, double)

static void BlockGath(const Strider *strider)
{
  Strider *next= strider->next;
  long stride= strider->stride;
  long i, n= strider->number;
  Array *indexList= strider->indexList;

  char *saddr0= saddr;

  if (!indexList) {
    if (next) {
      for (i=0 ; i<n ; i++) {
        BlockGath(next);
        saddr+= stride;
        /* daddr already incremented by other branches of BlockGath */
      }
    } else if (FastGath) {
      FastGath(stride, n, (long *)0);
    } else if (stride!=size) {
      for (i=0 ; i<n ; i++) {
        Copy(copyBase, daddr, saddr, 1L);
        saddr+= stride;
        daddr+= size;
      }
    } else {
      Copy(copyBase, daddr, saddr, n);
      n*= size;
      daddr+= n;
    }

  } else {
    long *list= indexList->value.l;
    if (next) {
      for (i=0 ; i<n ; i++) {
        saddr= saddr0+stride*list[i];
        BlockGath(next);
        /* daddr already incremented by other branches of BlockGath */
      }
    } else if (FastGath) {
      FastGath(stride, n, list);
    } else {
      if (size==stride) {
        long j, li, ni;
        for (i=0 ; i<n ; i++) {
          ni= 1;  /* "where" function often leaves sequential lists */
          li= list[i];
          for (j=li+1 ; i+1<n && j==list[i+1] ; i++, j++) ni++;
          Copy(copyBase, daddr, saddr+stride*li, ni);
          daddr+= size*ni;
        }
      } else {
        for (i=0 ; i<n ; i++) {
          Copy(copyBase, daddr, saddr+stride*list[i], 1L);
          daddr+= size;
        }
      }
    }
  }

  /* Always leave saddr as we found it for proper recursion. */
  saddr= saddr0;
}

void Gather(void *dst, void *src, StructDef *base, long number,
            const Strider *strider)
{
  if (strider) {
    saddr= src;
    daddr= dst;
    FastGath= FGathers[base->dataOps->promoteID];
    if (!FastGath) {
      copyBase= base;
      Copy= base->Copy;
    }
    size= base->size;
    BlockGath(strider);
  } else {
    /* 0 Strider* uses the specified number instead */
    base->Copy(base, dst, src, number);
  }
}

/*--------------------------------------------------------------------------*/

static IOStream *file;

/* Reuse BlockScat code by creating a fake copy operation.
   This trick relies on (char* + long) - (char*) always returning the
   original long -- may not be true if there is some machine with a
   long longer than the largest char*...  */
static Copier CopyWrite;
static void CopyWrite(StructDef *base, void *dst, const void *src, long n)
{
  long trick= ((char *)dst) - ((char *)0);
  YcWrite(file, src, trick, n*size);
}

void CastWrite(void *src, long dst, StructDef *base, long number,
               const Strider *strider)
{
  char *trick= ((char *)0) + dst;
  file= base->file;   /* fails disastrously if this is 0 */
  size= base->size;
  if (!(file->permissions & 2))
    YErrorIO("attempt to write to read-only file");
  if (strider) {
    saddr= src;
    daddr= trick;
    copyBase= base;
    Copy= &CopyWrite;
    FastScat= 0;
    BlockScat(strider);
  } else {
    /* 0 Strider* uses the specified number instead */
    CopyWrite(base, trick, src, number);
  }
}

/* Reuse BlockGath code by creating a fake copy operation.
   This trick relies on (char* + long) - (char*) always returning the
   original long -- may not be true if there is some machine with a
   long longer than the largest char*...  */
static Copier CopyRead;
static void CopyRead(StructDef *base, void *dst, const void *src, long n)
{
  long trick= ((char *)src) - ((char *)0);
  n*= size;
  if (YcRead(file, dst, trick, n) < n)
    YErrorIO("encountered end-of-file before read completed");
}

void CastRead(void *dst, long src, StructDef *base, long number,
              const Strider *strider)
{
  char *trick= ((char *)0) + src;
  file= base->file;   /* fails disastrously if this is 0 */
  size= base->size;
  if (!(file->permissions & 1))
    YErrorIO("attempt to read from write-only file");
  if (strider) {
    saddr= trick;
    daddr= dst;
    copyBase= base;
    Copy= &CopyRead;
    FastGath= 0;
    BlockGath(strider);
  } else {
    /* 0 Strider* uses the specified number instead */
    CopyRead(base, dst, trick, number);
  }
}

/*--------------------------------------------------------------------------*/