File indexing completed on 2023-05-28 05:10:56 UTC
view on githubraw file Latest commit b4daa243 on 2023-05-28 03:53:22 UTC
b4daa24319 Shre*0001 #include <stdio.h>
0002 #include <stdlib.h>
0003 #include "omp.h"
0004 #include "adStack.h"
0005 #include "adOMP.h"
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 void assertSafeStack() {
0017 if(!stackIsThreadSafe()) {
0018 printf("ERROR: OpenMP-parallel derivative program uses non-OpenMP ADFirstAidKit.\n") ;
0019 printf("Update to the latest ADFirstAidKit and compile it with openmp flags enabled.\n") ;
0020 exit(1) ;
0021 }
0022 }
0023
0024
0025
0026
0027
0028
0029
0030 void getStaticSchedule(int lo, int hi, int stride, int* threadlo, int* threadhi) {
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 int trip_count = (int)((hi-lo+stride)/stride) ;
0041 if(trip_count < 0) trip_count = 0 ;
0042
0043 int nth = omp_get_num_threads() ;
0044 int tid = omp_get_thread_num() ;
0045
0046 if(trip_count < nth) {
0047
0048
0049 if(tid < trip_count) {
0050
0051 *threadlo = lo + tid * stride ;
0052 *threadhi = *threadlo ;
0053 }
0054 else {
0055
0056 *threadhi = 0 ;
0057 *threadlo = *threadhi + stride ;
0058 }
0059 }
0060
0061
0062
0063
0064 else {
0065 int chunksize = trip_count / nth ;
0066 int extras = trip_count % nth ;
0067 int tidextras, incr ;
0068 if(tid < extras) {
0069 tidextras = tid ;
0070 incr = 0 ;
0071 }
0072 else {
0073 tidextras = extras ;
0074 incr = stride ;
0075 }
0076 *threadlo = lo + (tid*chunksize + tidextras) * stride ;
0077 *threadhi = *threadlo + chunksize * stride - incr ;
0078 }
0079 assertSafeStack() ;
0080 }
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 static int numChunks, previousIter, thisChunkStart ;
0091 static char isFirstIter ;
0092 #pragma omp threadprivate(numChunks, previousIter, isFirstIter, thisChunkStart)
0093
0094 void initDynamicSchedule() {
0095 isFirstIter = 1 ;
0096 numChunks = 0 ;
0097 assertSafeStack() ;
0098 }
0099
0100 void recordDynamicSchedule(int counter, int stride) {
0101 if(isFirstIter) {
0102 thisChunkStart = counter ;
0103 isFirstIter = 0 ;
0104 numChunks ++ ;
0105 }
0106 else {
0107 if(previousIter + stride != counter) {
0108 pushInteger4(thisChunkStart) ;
0109 pushInteger4(previousIter) ;
0110 thisChunkStart = counter ;
0111 numChunks ++ ;
0112 }
0113 }
0114 previousIter = counter ;
0115 }
0116
0117 void finalizeDynamicSchedule() {
0118 pushInteger4(thisChunkStart) ;
0119 pushInteger4(previousIter) ;
0120 pushInteger4(numChunks) ;
0121 }
0122
0123
0124
0125 void assertsafestack_() {
0126 assertSafeStack() ;
0127 }
0128
0129 void getstaticschedule_(int* lo, int* hi, int* stride, int* threadlo, int* threadhi) {
0130 getStaticSchedule(*lo, *hi, *stride, threadlo, threadhi) ;
0131 }
0132
0133 void initdynamicschedule_() {
0134 initDynamicSchedule() ;
0135 }
0136
0137 void recorddynamicschedule_(int* counter, int* stride) {
0138 recordDynamicSchedule(*counter, *stride) ;
0139 }
0140
0141 void finalizedynamicschedule_() {
0142 finalizeDynamicSchedule() ;
0143 }