00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __omp_rtl_thread_included
00035 #define __omp_rtl_thread_included
00036
00037 #include "omp_rtl.h"
00038 #include "omp_sys.h"
00039 #include "omp_util.h"
00040
00041
00042
00043
00044
00045
00046 extern pthread_mutex_t __omp_hash_table_lock;
00047
00048 inline void __ompc_set_nested(const int __nested)
00049 {
00050
00051 __omp_nested = __nested;
00052 }
00053
00054 inline void __ompc_set_dynamic(const int __dynamic)
00055 {
00056 __omp_dynamic = __dynamic;
00057 }
00058
00059 inline int __ompc_get_dynamic(void)
00060 {
00061 return __omp_dynamic;
00062 }
00063
00064 inline int __ompc_get_nested(void)
00065 {
00066 return __omp_nested;
00067 }
00068
00069 inline int __ompc_get_max_threads(void)
00070 {
00071 if (__omp_rtl_initialized == 1)
00072 return __omp_nthreads_var;
00073 else
00074 return Get_SMP_CPU_num();
00075 }
00076
00077 inline int __ompc_get_num_procs(void)
00078 {
00079 if (__omp_rtl_initialized == 1)
00080 return __omp_num_processors;
00081 else
00082 return Get_SMP_CPU_num();
00083 }
00084
00085
00086 inline void __ompc_set_num_threads(const int __num_threads)
00087 {
00088
00089 Is_Valid( __num_threads > 0,
00090 (" number of threads must be possitive!"));
00091 if (__num_threads > __omp_max_num_threads) {
00092 Warning(" Exceed the threads number limit.");
00093 }
00094
00095 __omp_nthreads_var = __num_threads;
00096 }
00097
00098 inline int __ompc_in_parallel(void)
00099 {
00100 return (__omp_exe_mode != OMP_EXE_MODE_SEQUENTIAL);
00101 }
00102
00103 static inline void __ompc_clear_hash_table(void)
00104 {
00105 memset(__omp_uthread_hash_table, 0, sizeof(__omp_uthread_hash_table));
00106 }
00107
00108 static inline void __ompc_insert_into_hash_table(omp_u_thread_t * new_u_thread)
00109 {
00110 omp_u_thread_t *u_thread_temp;
00111 int hash_index;
00112 pthread_t uthread_id = new_u_thread->uthread_id;
00113
00114 hash_index = HASH_IDX(uthread_id);
00115
00116 pthread_mutex_lock(&__omp_hash_table_lock);
00117 u_thread_temp = __omp_uthread_hash_table[hash_index];
00118
00119 __omp_uthread_hash_table[hash_index] = new_u_thread;
00120 new_u_thread->hash_next = u_thread_temp;
00121 pthread_mutex_unlock(&__omp_hash_table_lock);
00122 }
00123
00124 static inline void __ompc_remove_from_hash_table(pthread_t uthread_id)
00125 {
00126 omp_u_thread_t *uthread_temp;
00127 int hash_index;
00128
00129 hash_index = HASH_IDX(uthread_id);
00130 pthread_mutex_lock(&__omp_hash_table_lock);
00131 uthread_temp = __omp_uthread_hash_table[hash_index];
00132 Is_True( uthread_temp != NULL, ("No such pthread in hash table"));
00133 if (uthread_temp->uthread_id == uthread_id)
00134 __omp_uthread_hash_table[hash_index] = uthread_temp->hash_next;
00135 else {
00136 omp_u_thread_t *uthread_next = uthread_temp->hash_next;
00137 while (uthread_next != NULL) {
00138 if (uthread_next->uthread_id == uthread_id) {
00139 uthread_temp->hash_next = uthread_next->hash_next;
00140 return;
00141 } else {
00142 uthread_temp = uthread_next;
00143 uthread_next = uthread_next->hash_next;
00144 }
00145 }
00146 Is_True(0, ("No such pthread in hash table"));
00147 }
00148 pthread_mutex_unlock(&__omp_hash_table_lock);
00149 }
00150
00151 inline omp_u_thread_t * __ompc_get_current_u_thread()
00152 {
00153 omp_u_thread_t *uthread_temp;
00154 pthread_t current_uthread_id;
00155
00156 Is_True(__omp_uthread_hash_table != NULL,
00157 ("RTL data structures haven't been initialized yet!"));
00158
00159 current_uthread_id = pthread_self();
00160 uthread_temp = __omp_uthread_hash_table[HASH_IDX(current_uthread_id)];
00161 Is_True(uthread_temp != NULL, ("This pThread is not in hash table!"));
00162
00163 if (uthread_temp->uthread_id == current_uthread_id)
00164 return uthread_temp;
00165 else {
00166 do {
00167 uthread_temp = uthread_temp->hash_next;
00168 Is_True(uthread_temp != NULL,
00169 ("This pThread is not in hash table!"));
00170 } while (uthread_temp->uthread_id != current_uthread_id);
00171 return uthread_temp;
00172 }
00173 }
00174
00175 inline omp_v_thread_t * __ompc_get_current_v_thread()
00176 {
00177 omp_v_thread_t *v_thread_temp;
00178
00179 v_thread_temp = __ompc_get_current_u_thread()->task;
00180 Is_True(v_thread_temp != NULL,
00181 ("task structure of u_thread not properly set!"));
00182 return v_thread_temp;
00183 }
00184
00185 inline omp_v_thread_t * __ompc_get_v_thread_by_num( int vthread_id )
00186 {
00187 omp_v_thread_t *v_thread_temp;
00188
00189
00190
00191
00192 if (__omp_exe_mode & OMP_EXE_MODE_NORMAL) {
00193 v_thread_temp = &(__omp_level_1_team[vthread_id]);
00194 Is_True(v_thread_temp != NULL,
00195 ("something wrong with level_1_team!"));
00196 return v_thread_temp;
00197 } else if (__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL) {
00198 return &__omp_root_v_thread;
00199 } else {
00200 return __ompc_get_current_v_thread();
00201 }
00202 }
00203
00204 inline int __ompc_get_local_thread_num(void)
00205 {
00206 if (__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL) {
00207 return 0;
00208 } else {
00209 return __ompc_get_current_v_thread()->vthread_id;
00210 }
00211 }
00212
00213 inline int __ompc_get_num_threads(void)
00214 {
00215 if (__omp_exe_mode & OMP_EXE_MODE_NORMAL) {
00216 return __omp_level_1_team_size;
00217 } else if (__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL) {
00218 return 1;
00219 } else {
00220 return __ompc_get_current_v_thread()->team_size;
00221 }
00222 }
00223
00224 inline omp_team_t * __ompc_get_current_team(void)
00225 {
00226 if (__omp_exe_mode & OMP_EXE_MODE_NORMAL)
00227 return &__omp_level_1_team_manager;
00228 else if(__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL) {
00229
00230
00231
00232
00233 return &__omp_root_team;
00234 } else {
00235 return __ompc_get_current_v_thread()->team;
00236 }
00237 }
00238
00239
00240 inline void __ompc_barrier_wait(omp_team_t *team)
00241 {
00242
00243 int barrier_flag;
00244 int reset_barrier = 0;
00245 int new_count;
00246 int i, j;
00247
00248 barrier_flag = team->barrier_flag;
00249 new_count = __ompc_atomic_inc(&team->barrier_count);
00250
00251 if (new_count == team->team_size) {
00252
00253 team->barrier_count = 0;
00254 team->barrier_count2 = 1;
00255 team->barrier_flag = barrier_flag ^ 1;
00256 for (i = 0; i < 300; i++)
00257 if (team->barrier_count2 == team->team_size) {
00258 return;
00259 }
00260 pthread_mutex_lock(&(team->barrier_lock));
00261 pthread_mutex_unlock(&(team->barrier_lock));
00262 pthread_cond_broadcast(&(team->barrier_cond));
00263 } else {
00264
00265
00266
00267 for (i = 0; i < 5000; i++)
00268 if (team->barrier_flag != barrier_flag) {
00269 __ompc_atomic_inc(&team->barrier_count2);
00270 return;
00271 }
00272 pthread_mutex_lock(&(team->barrier_lock));
00273 while (team->barrier_flag == barrier_flag)
00274 pthread_cond_wait(&(team->barrier_cond), &(team->barrier_lock));
00275 pthread_mutex_unlock(&(team->barrier_lock));
00276 }
00277
00278 }
00279
00280
00281
00282 inline void __ompc_barrier(void)
00283 {
00284 omp_v_thread_t *temp_v_thread;
00285 if (__omp_exe_mode & OMP_EXE_MODE_NORMAL) {
00286
00287
00288 __ompc_barrier_wait(&__omp_level_1_team_manager);
00289 return;
00290
00291
00292
00293 } else if (__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL)
00294 return;
00295
00296
00297 temp_v_thread = __ompc_get_current_v_thread();
00298 if(temp_v_thread->team_size == 1)
00299 return;
00300 else {
00301 __ompc_barrier_wait(temp_v_thread->team);
00302 }
00303 }
00304
00305
00306 int
00307 __ompc_check_num_threads(const int _num_threads)
00308 {
00309 int num_threads = _num_threads;
00310 int request_threads;
00311
00312 Is_Valid( num_threads > 0,
00313 (" number of threads must be possitive!"));
00314 if (__omp_exe_mode & OMP_EXE_MODE_SEQUENTIAL) {
00315
00316 request_threads = num_threads - __omp_level_1_team_alloc_size;
00317 if (request_threads <= __omp_max_num_threads) {
00318
00319 } else {
00320
00321 Warning(" Exceed the thread number limit: Reduce to Max");
00322 num_threads = __omp_level_1_team_alloc_size + __omp_max_num_threads;
00323 }
00324 } else {
00325 if ((num_threads - 1) > __omp_max_num_threads) {
00326
00327
00328 num_threads = __omp_max_num_threads + 1;
00329 Warning(" Exceed the thread number limit: Reduce to Max");
00330 } else {
00331
00332 }
00333 }
00334 return num_threads;
00335 }
00336
00337
00338
00339 inline void __ompc_flush(void *p)
00340 {
00341
00342 }
00343
00344
00345 inline int __ompc_can_fork(void)
00346 {
00347
00348 return 1;
00349 }
00350
00351 inline void __ompc_begin(void)
00352 {
00353
00354 }
00355
00356 inline void __ompc_end(void)
00357 {
00358
00359 }
00360
00361
00362 #endif