| 1 | package OpenMP::Simple; | ||
| 2 | |||
| 3 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use Alien::OpenMP; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 9 | our $VERSION = q{0.0.3}; | |
| 8 | |||
| 9 | sub Inline { | ||
| 10 | 7 | ▶ | my ($self, $lang) = @_; |
|
Subroutine
Inline: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 11 | 7 | my $config = Alien::OpenMP->Inline($lang); | |
| 12 | 7 | $config->{AUTO_INCLUDE} .=q{ | |
| 13 | |||
| 14 | /** | ||
| 15 | All setters EXCEPT the LOCK retoutines, | ||
| 16 | as defined at https://gcc.gnu.org/onlinedocs/libgomp/Runtime-Library-Routines.html | ||
| 17 | |||
| 18 | Status GCC* Description | ||
| 19 | +-------------------------------------------------------------------------------------------+ | ||
| 20 | - DONE omp_set_num_threads 8.3.0 – Set upper team size limit | ||
| 21 | - DONE omp_set_schedule 8.3.0 – Set the runtime scheduling method | ||
| 22 | - DONE omp_set_dynamic 8.3.0 – Enable/disable dynamic teams | ||
| 23 | - DONE omp_set_nested 8.3.0 – Enable/disable nested parallel regions | ||
| 24 | - DONE omp_set_max_active_levels 8.3.0 – Limits the number of active parallel regions | ||
| 25 | - DONE omp_set_default_device 8.3.0 – Set the default device for target regions | ||
| 26 | - DONE omp_set_num_teams 12.3.0 – Set upper teams limit for teams construct | ||
| 27 | - DONE omp_set_teams_thread_limit 12.3.0 – Set upper thread limit for teams construct | ||
| 28 | +-------------------------------------------------------------------------------------------+ | ||
| 29 | **/ | ||
| 30 | |||
| 31 | /* %ENV Update Macros */ | ||
| 32 | |||
| 33 | #define PerlOMP_ENV_SET_DEFAULT_DEVICE \ | ||
| 34 | char *num = getenv("OMP_DEFAULT_DEVICE"); \ | ||
| 35 | omp_set_default_device(atoi(num)); ///< read and update with $ENV{OMP_NUM_TEAMS} | ||
| 36 | |||
| 37 | #define PerlOMP_ENV_SET_TEAMS_THREAD_LIMIT \ | ||
| 38 | char *num = getenv("OMP_TEAMS_THREAD_LIMIT"); \ | ||
| 39 | omp_set_teams_thread_limit(atoi(num)); ///< read and update with $ENV{OMP_NUM_TEAMS} | ||
| 40 | |||
| 41 | #define PerlOMP_ENV_SET_NUM_TEAMS \ | ||
| 42 | char *num = getenv("OMP_NUM_TEAMS"); \ | ||
| 43 | omp_set_num_teams(atoi(num)); ///< read and update with $ENV{OMP_NUM_TEAMS} | ||
| 44 | |||
| 45 | #define PerlOMP_ENV_SET_MAX_ACTIVE_LEVELS \ | ||
| 46 | char *num = getenv("OMP_MAX_ACTIVE_LEVELS"); \ | ||
| 47 | omp_set_max_active_levels(atoi(num)); ///< read and update with $ENV{OMP_MAX_ACTIVE_LEVELS} | ||
| 48 | |||
| 49 | #define PerlOMP_ENV_SET_NUM_THREADS \ | ||
| 50 | char *num = getenv("OMP_NUM_THREADS"); \ | ||
| 51 | omp_set_num_threads(atoi(num)); ///< read and update with $ENV{OMP_NUM_THREADS} | ||
| 52 | |||
| 53 | #define PerlOMP_ENV_SET_DYNAMIC \ | ||
| 54 | char *VALUE = getenv("OMP_DYNAMIC"); \ | ||
| 55 | if (VALUE == NULL) { \ | ||
| 56 | omp_set_dynamic(VALUE); \ | ||
| 57 | } \ | ||
| 58 | else if (strcmp(VALUE,"TRUE") || strcmp(VALUE,"true") || strcmp(VALUE,"1")) { \ | ||
| 59 | omp_set_dynamic(1); \ | ||
| 60 | } \ | ||
| 61 | else { \ | ||
| 62 | omp_set_dynamic(NULL); \ | ||
| 63 | }; ///> read and update with $ENV{OMP_DYNAMIC} | ||
| 64 | |||
| 65 | #define PerlOMP_ENV_SET_NESTED \ | ||
| 66 | char *VALUE = getenv("OMP_NESTED"); \ | ||
| 67 | if (VALUE == NULL) { \ | ||
| 68 | omp_set_nested(VALUE); \ | ||
| 69 | } \ | ||
| 70 | else if (strcmp(VALUE,"TRUE") || strcmp(VALUE,"true") || strcmp(VALUE,"1")) { \ | ||
| 71 | omp_set_nested(1); \ | ||
| 72 | } \ | ||
| 73 | else { \ | ||
| 74 | omp_set_nested(NULL); \ | ||
| 75 | }; ///> read and update with $ENV{OMP_NESTED} | ||
| 76 | |||
| 77 | #define PerlOMP_ENV_SET_SCHEDULE \ | ||
| 78 | char *str = getenv("OMP_SCHEDULE"); \ | ||
| 79 | omp_sched_t SCHEDULE = omp_sched_static; \ | ||
| 80 | int CHUNK = 1; char *pt; \ | ||
| 81 | pt = strtok (str,","); \ | ||
| 82 | if (strcmp(pt,"static")) { \ | ||
| 83 | SCHEDULE = omp_sched_static; \ | ||
| 84 | } \ | ||
| 85 | else if (strcmp(pt,"dynamic")) { \ | ||
| 86 | SCHEDULE = omp_sched_dynamic; \ | ||
| 87 | } \ | ||
| 88 | else if (strcmp(pt,"guided")) { \ | ||
| 89 | SCHEDULE = omp_sched_guided; \ | ||
| 90 | } \ | ||
| 91 | else if (strcmp(pt,"auto")) { \ | ||
| 92 | SCHEDULE = omp_sched_auto; \ | ||
| 93 | } \ | ||
| 94 | pt = strtok (NULL, ","); \ | ||
| 95 | if (pt != NULL) { \ | ||
| 96 | CHUNK = atoi(pt); \ | ||
| 97 | } \ | ||
| 98 | omp_set_schedule(SCHEDULE, CHUNK); ///< read and update with $ENV{OMP_SCHEDULE} | ||
| 99 | |||
| 100 | // ... add all of them from OpenMP::Environment, add unit tests | ||
| 101 | |||
| 102 | /* Output Init Macros (needed?) */ | ||
| 103 | #define PerlOMP_RET_ARRAY_REF_ret AV* ret = newAV();sv_2mortal((SV*)ret); | ||
| 104 | |||
| 105 | /* Datatype Converters (doxygen style comments) */ | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Converts a 1D Perl Array Reference (AV*) into a 1D C array of floats; allocates retArray[numElements] by reference | ||
| 109 | * @param[in] *Aref, int numElements, float retArray[numElements] | ||
| 110 | * @param[out] void | ||
| 111 | */ | ||
| 112 | |||
| 113 | void PerlOMP_1D_Array_TO_FLOAT_ARRAY_1D(SV *Aref, int numElements, float retArray[numElements]) { | ||
| 114 | for (int i=0; i<numElements; i++) { | ||
| 115 | SV **element = av_fetch((AV*)SvRV(Aref), i, 0); | ||
| 116 | retArray[i] = SvNV(*element); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | /* 2D AoA to 2D float C array ... | ||
| 121 | * Convert a regular MxN Perl array of arrays (AoA) consisting of floating point values, e.g., | ||
| 122 | * | ||
| 123 | * my $AoA = [ [qw/1.01 2.02 3.03/], [qw/3.145 2.123 0.892/], [qw/19.17 60.651 20.17/] ]; | ||
| 124 | * | ||
| 125 | * into a C array of the same dimensions so that it can be used as expected with an OpenMP | ||
| 126 | * "#pragma omp for" work sharing construct | ||
| 127 | */ | ||
| 128 | |||
| 129 | // contribued by CPAN's NERDVANA - thank you! | ||
| 130 | void PerlOMP_2D_AoA_TO_FLOAT_ARRAY_2D(SV *AoA, int numRows, int rowSize, float retArray[numRows][rowSize]) { | ||
| 131 | SV **AVref; | ||
| 132 | if (!SvROK(AoA) || SvTYPE(SvRV(AoA)) != SVt_PVAV) | ||
| 133 | croak("Expected Arrayref"); | ||
| 134 | for (int i=0; i<numRows; i++) { | ||
| 135 | AVref = av_fetch((AV*)SvRV(AoA), i, 0); | ||
| 136 | if (!AVref || !*AVref || !SvROK(*AVref) || SvTYPE(SvRV(*AVref)) != SVt_PVAV) | ||
| 137 | croak("Expected arrayref at array[%d]", i); | ||
| 138 | for (int j=0; j<rowSize;j++) { | ||
| 139 | SV **element = av_fetch((AV*)SvRV(*AVref), j, 0); | ||
| 140 | if (!element || !*element || !SvOK(*element)) | ||
| 141 | croak("Expected value at array[%d][%d]", i, j); | ||
| 142 | retArray[i][j] = SvNV(*element); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | /* 1D Array reference to 1D int C array ... | ||
| 148 | * Convert a regular M-element Perl array consisting of inting point values, e.g., | ||
| 149 | * | ||
| 150 | * my $Aref = [ 10, 314, 527, 911, 538 ]; | ||
| 151 | * | ||
| 152 | * into a C array of the same dimensions so that it can be used as exepcted with an OpenMP | ||
| 153 | * "#pragma omp for" work sharing construct | ||
| 154 | */ | ||
| 155 | |||
| 156 | void PerlOMP_1D_Array_TO_INT_ARRAY_1D(SV *Aref, int numElements, int retArray[numElements]) { | ||
| 157 | for (int i=0; i<numElements; i++) { | ||
| 158 | SV **element = av_fetch((AV*)SvRV(Aref), i, 0); | ||
| 159 | retArray[i] = SvIV(*element); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | /* 2D AoA to 2D int C array ... | ||
| 164 | * Convert a regular MxN Perl array of arrays (AoA) consisting of inting point values, e.g., | ||
| 165 | * | ||
| 166 | * my $AoA = [ [qw/101 202 303/], [qw/3145 2123 892/], [qw/1917 60.651 2017/] ]; | ||
| 167 | * | ||
| 168 | * into a C array of the same dimensions so that it can be used as expected with an OpenMP | ||
| 169 | * "#pragma omp for" work sharing construct | ||
| 170 | */ | ||
| 171 | |||
| 172 | void PerlOMP_2D_AoA_TO_INT_ARRAY_2D(SV *AoA, int numRows, int rowSize, int retArray[numRows][rowSize]) { | ||
| 173 | SV **AVref; | ||
| 174 | for (int i=0; i<numRows; i++) { | ||
| 175 | AVref = av_fetch((AV*)SvRV(AoA), i, 0); | ||
| 176 | for (int j=0; j<rowSize;j++) { | ||
| 177 | SV **element = av_fetch((AV*)SvRV(*AVref), j, 0); | ||
| 178 | retArray[i][j] = SvIV(*element); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | /* TODO: | ||
| 184 | * add unit tests for conversion functions | ||
| 185 | * add some basic matrix operations (transpose for 2D, reverse for 1D) | ||
| 186 | * experiment with simple hash ref to C struct | ||
| 187 | * ... | ||
| 188 | */ | ||
| 189 | |||
| 190 | }; | ||
| 191 | 7 | return $config; | |
| 192 | } | ||
| 193 | |||
| 194 | 9 | 1; | |
| 195 | |||
| 196 | __END__ | ||