home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume41 / parallel / part01 / parallel.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-19  |  5.3 KB  |  141 lines

  1. /***************************************************************************
  2.  The following is...
  3.       Copyright (C) 1993 by Macquarie University
  4.       Released without ANY warranty to the public domain.
  5.       Written by Richard Larkin. rlarkin@zen.efs.mq.edu.au
  6.  
  7.       Thanks go to Mark Hahn for help with <sys/mman.h> functions.
  8.  
  9.  parallel.h header for parallel.c
  10.  
  11.  Basic idea...
  12.  -------------
  13.  Somewhere call
  14.  ParallelProcessors(N)
  15.  with N the number of processors you have.
  16.  
  17.  For arrays that have to be globally written to within parallel loops,
  18.  use ParallelCalloc()/ParallelFree() instead of calloc()/free().
  19.  Also use for huge (non written) arrays to save on memory usage.
  20.  
  21.  For loops to parallelise of the form...
  22.  {Only nonrecurrent loops can be parallelised.}
  23.         for (i = 0; i < MAXIMUM; i++) {do_somethings};
  24.  Change to...
  25.         ParallelSplit(MAXIMUM, &min, &max);
  26.         for (i = min; i < max; i++) {do_somethings};
  27.         ParallelRegroup();
  28.  
  29.  NOTES: All variables that are not Parallelled,
  30.         will be local to each processor.
  31.  
  32.         Nested parallelising is not supported.
  33.  
  34.         ParallelCalloc()/ParallelFree() should not be used within
  35.         parallelled sections of code.
  36.  
  37.  Overheads: If do_somethings takes time enough to warrant being
  38.             parallelised, the overheads are not worth mentioning.
  39.  
  40.             Under MS-DOS the overhead is 2 redundant stores.
  41.  
  42.             Any single processor machine will use normal calloc'ed
  43.             memory and do approximately 4 simple conditional calculations
  44.             and 2 redundant stores per Split/Regroup.
  45.  ***************************************************************************/
  46.  
  47. #ifndef __PARALLEL_HEADER
  48. #define __PARALLEL_HEADER
  49.  
  50. #ifndef MSDOS
  51.  
  52. #include <stddef.h>
  53. #include <stdio.h>
  54. #include <fcntl.h>
  55. #include <sys/wait.h>
  56. #include <sys/types.h>
  57. #include <sys/mman.h>
  58.  
  59. /* Until I find where they should be !!!! */
  60. #define NULL  0
  61. #define TRUE  (0==0)
  62. #define FALSE (0!=0)
  63. #define Realloc(P, s)  (P == NULL ? malloc((s)) : realloc((P), (size_t) (s)))
  64. int write(int fd, char *buf, int nbyte);
  65. int close (int fd);
  66. int fork(void);
  67. int munmap(caddr_t addr, int len);
  68. int madvise(caddr_t addr, size_t len, int advice);
  69. int unlink(char *path);
  70. void *malloc(size_t S);
  71. void *calloc(size_t N, size_t S);
  72. void *realloc(void *P, size_t S);
  73.  
  74. /***************************************************************************
  75.  ParallelProcessors tell parallel routines how many processors to assume.
  76.  Pre : none.
  77.  Post: Internal knowledge of number of processors set to maximum of N and 1.
  78.  ***************************************************************************/
  79. void ParallelProcessors(long int N);
  80.  
  81.  
  82. /***************************************************************************
  83.  ParallelSplit split into useful number of processes and return
  84.  interval of relevance to each process.
  85.  Calls to ParallelSplit cannot be nested.
  86.  Pre : Min, Max not NULL. NumberProcUsed == 1.
  87.  Post: NumberProcUsed is number of active processes.
  88.        [Min..Max) will be interval of Range to use for each process.
  89.  ***************************************************************************/
  90. void ParallelSplit(long int Range, long int *Min, long int *Max);
  91.  
  92.  
  93. /***************************************************************************
  94.  ParallelRegroup children all return to their parent who waits for them.
  95.  Pre : NumberProcUsed is number of active processes.
  96.  Post: One parent process. NumberProcUsed = 1;
  97.  ***************************************************************************/
  98. void ParallelRegroup(void);
  99.  
  100.  
  101. /***************************************************************************
  102.  ParallelCalloc allocated a piece of memory to be globally accessible
  103.  throughout all processors.
  104.  Pre : none.
  105.  Post: Returns pointer to memory mapped parallel array file or
  106.        in case of trivial processors returns normal Calloc result.
  107.        Null returned on failure.
  108.  ***************************************************************************/
  109. void *ParallelCalloc(size_t nitems, size_t size);
  110.  
  111. /***************************************************************************
  112.  ParallelFree frees a piece of memory associated with a ParallelCalloc.
  113.  Pre : P is pointer return from ParallelCalloc().
  114.  Post: Memory and possible file are freed.
  115.  ***************************************************************************/
  116. void _ParallelFree(void *P);
  117. #define ParallelFree(P)           _ParallelFree((void *) P)
  118.  
  119. #else
  120.  
  121. /* Dummies for MSDOS                                                       */
  122. #define ParallelProcessors(N)             {}
  123. #define ParallelSplit(Range, Min, Max)    {*Min = 0; *Max = Range;}
  124. #define ParallelRegroup()                 {}
  125. #define ParallelCalloc(nitems, size)      calloc(nitems, size)
  126. #define ParallelFree(P)                   free(P)
  127.  
  128. #endif 
  129.  
  130. /* Better access to parallel memory allocation.                            */
  131. #define ParallelTypeMalloc(T, n)                                            \
  132.           (T *) (ParallelCalloc((size_t)(n)*sizeof(T)), 1)
  133. #define ParallelTypeCalloc(T, n)                                            \
  134.           (T *) (ParallelCalloc((size_t)(n), sizeof(T)))
  135.  
  136. #endif /* __PARALLEL_HEADER */
  137.  
  138. /***************************************************************************
  139.  End of parallel.h
  140.  ***************************************************************************/
  141.