home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- The following is...
- Copyright (C) 1993 by Macquarie University
- Released without ANY warranty to the public domain.
- Written by Richard Larkin. rlarkin@zen.efs.mq.edu.au
-
- Thanks go to Mark Hahn for help with <sys/mman.h> functions.
-
- parallel.h header for parallel.c
-
- Basic idea...
- -------------
- Somewhere call
- ParallelProcessors(N)
- with N the number of processors you have.
-
- For arrays that have to be globally written to within parallel loops,
- use ParallelCalloc()/ParallelFree() instead of calloc()/free().
- Also use for huge (non written) arrays to save on memory usage.
-
- For loops to parallelise of the form...
- {Only nonrecurrent loops can be parallelised.}
- for (i = 0; i < MAXIMUM; i++) {do_somethings};
- Change to...
- ParallelSplit(MAXIMUM, &min, &max);
- for (i = min; i < max; i++) {do_somethings};
- ParallelRegroup();
-
- NOTES: All variables that are not Parallelled,
- will be local to each processor.
-
- Nested parallelising is not supported.
-
- ParallelCalloc()/ParallelFree() should not be used within
- parallelled sections of code.
-
- Overheads: If do_somethings takes time enough to warrant being
- parallelised, the overheads are not worth mentioning.
-
- Under MS-DOS the overhead is 2 redundant stores.
-
- Any single processor machine will use normal calloc'ed
- memory and do approximately 4 simple conditional calculations
- and 2 redundant stores per Split/Regroup.
- ***************************************************************************/
-
- #ifndef __PARALLEL_HEADER
- #define __PARALLEL_HEADER
-
- #ifndef MSDOS
-
- #include <stddef.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/mman.h>
-
- /* Until I find where they should be !!!! */
- #define NULL 0
- #define TRUE (0==0)
- #define FALSE (0!=0)
- #define Realloc(P, s) (P == NULL ? malloc((s)) : realloc((P), (size_t) (s)))
- int write(int fd, char *buf, int nbyte);
- int close (int fd);
- int fork(void);
- int munmap(caddr_t addr, int len);
- int madvise(caddr_t addr, size_t len, int advice);
- int unlink(char *path);
- void *malloc(size_t S);
- void *calloc(size_t N, size_t S);
- void *realloc(void *P, size_t S);
-
- /***************************************************************************
- ParallelProcessors tell parallel routines how many processors to assume.
- Pre : none.
- Post: Internal knowledge of number of processors set to maximum of N and 1.
- ***************************************************************************/
- void ParallelProcessors(long int N);
-
-
- /***************************************************************************
- ParallelSplit split into useful number of processes and return
- interval of relevance to each process.
- Calls to ParallelSplit cannot be nested.
- Pre : Min, Max not NULL. NumberProcUsed == 1.
- Post: NumberProcUsed is number of active processes.
- [Min..Max) will be interval of Range to use for each process.
- ***************************************************************************/
- void ParallelSplit(long int Range, long int *Min, long int *Max);
-
-
- /***************************************************************************
- ParallelRegroup children all return to their parent who waits for them.
- Pre : NumberProcUsed is number of active processes.
- Post: One parent process. NumberProcUsed = 1;
- ***************************************************************************/
- void ParallelRegroup(void);
-
-
- /***************************************************************************
- ParallelCalloc allocated a piece of memory to be globally accessible
- throughout all processors.
- Pre : none.
- Post: Returns pointer to memory mapped parallel array file or
- in case of trivial processors returns normal Calloc result.
- Null returned on failure.
- ***************************************************************************/
- void *ParallelCalloc(size_t nitems, size_t size);
-
- /***************************************************************************
- ParallelFree frees a piece of memory associated with a ParallelCalloc.
- Pre : P is pointer return from ParallelCalloc().
- Post: Memory and possible file are freed.
- ***************************************************************************/
- void _ParallelFree(void *P);
- #define ParallelFree(P) _ParallelFree((void *) P)
-
- #else
-
- /* Dummies for MSDOS */
- #define ParallelProcessors(N) {}
- #define ParallelSplit(Range, Min, Max) {*Min = 0; *Max = Range;}
- #define ParallelRegroup() {}
- #define ParallelCalloc(nitems, size) calloc(nitems, size)
- #define ParallelFree(P) free(P)
-
- #endif
-
- /* Better access to parallel memory allocation. */
- #define ParallelTypeMalloc(T, n) \
- (T *) (ParallelCalloc((size_t)(n)*sizeof(T)), 1)
- #define ParallelTypeCalloc(T, n) \
- (T *) (ParallelCalloc((size_t)(n), sizeof(T)))
-
- #endif /* __PARALLEL_HEADER */
-
- /***************************************************************************
- End of parallel.h
- ***************************************************************************/
-