int test_kmp_set_defaults_lock_bug() { /* checks that omp_get_num_threads is equal to the number of threads */ int nthreads_lib; int nthreads = 0; nthreads_lib = -1; #pragma omp parallel { omp_set_lock(&lock); nthreads++; omp_unset_lock(&lock); #pragma omp single { nthreads_lib = omp_get_num_threads (); } /* end of single */ } /* end of parallel */ kmp_set_defaults("OMP_NUM_THREADS"); #pragma omp parallel { omp_set_lock(&lock); nthreads++; omp_unset_lock(&lock); } /* end of parallel */ return (nthreads == 2*nthreads_lib); }
/* Assumes node is already locked */ static inline void lockTwoNodes(Node *node, Node *node2) { IDnum nodeID = getNodeID(node); IDnum node2ID = getNodeID(node2); if (nodeID < 0) nodeID = -nodeID; if (node2ID < 0) node2ID = -node2ID; if (nodeID == node2ID) return; /* Lock lowest ID first to avoid deadlocks */ if (nodeID < node2ID) { omp_set_lock (nodeLocks + node2ID); } else if (!omp_test_lock (nodeLocks + node2ID)) { omp_unset_lock (nodeLocks + nodeID); omp_set_lock (nodeLocks + node2ID); omp_set_lock (nodeLocks + nodeID); } }
void multWith_LockedArrays(){ #pragma omp for schedule(static, N / NUM_THREADS) for(int i = 0; i < N; i++){ forces[i][X_Pos] = 0; forces[i][Y_Pos] = 0; } #pragma omp for schedule(static, N / NUM_THREADS) for(int i = 0; i < N; i++){ double x_dif, y_dif, dist, dist_cubed; double tempForcX, tempForcY; for(int j = i + 1; j < N; j++){ x_dif = pos[i][X_Pos] - pos[j][X_Pos]; y_dif = pos[i][Y_Pos] - pos[j][Y_Pos]; dist = sqrt(x_dif * x_dif + y_dif * y_dif); dist_cubed = dist * dist * dist; tempForcX = constG * masses[i] * masses[j] / dist_cubed * x_dif; tempForcY = constG * masses[i] * masses[j] / dist_cubed * y_dif; omp_set_lock(&locks[i]); forces[i][X_Pos] += tempForcX; forces[i][Y_Pos] += tempForcY; omp_unset_lock(&locks[i]); omp_set_lock(&locks[j]); forces[j][X_Pos] -= tempForcX; forces[j][Y_Pos] -= tempForcY; omp_unset_lock(&locks[j]); } } }
int kernel(int npoints, int nclusters) { int i, j, delta = 0, index; #pragma omp parallel for default(shared) \ private(i,j,index) \ schedule(static) for(i = 0; i < npoints; i++) { index = find_nearest_point(feature[i], 2, clusters, nclusters); if (membership[i] != index) { omp_set_lock(&delta_lock); delta += 1; omp_unset_lock(&delta_lock); } membership[i] = index; omp_set_lock(&new_centers_len_locks[index]); new_centers_len[index]++; omp_unset_lock(&new_centers_len_locks[index]); for(j = 0; j < 2; j++) { omp_set_lock(&new_centers_locks[index][j]); new_centers[index][j] += feature[i][j]; omp_unset_lock(&new_centers_locks[index][j]); } } return delta; }
int main(){ /*объявляем mutex*/ omp_lock_t lock_1; /*!!!обязательно инициализируем его!!!*/ omp_init_lock(&lock_1); #pragma omp parallel num_threads(3) { #pragma omp sections { #pragma omp section { omp_set_lock(&lock_1); //устанавливать и снимать надо в одном и том же потоке std::cout<<"После установки блокировки 1\n"; omp_unset_lock(&lock_1); } #pragma omp section { omp_set_lock(&lock_1); std::cout<<"После установки блокировки 2\n"; omp_unset_lock(&lock_1); } #pragma omp section { omp_set_lock(&lock_1); std::cout<<"После установки блокировки 3\n"; omp_unset_lock(&lock_1); } } } return 0; }
int dequeue(){ while(tail == NULL); if(head == tail){ int data = head->data; omp_set_lock(&head_lock); head = NULL; #pragma omp flush(head) omp_unset_lock(&head_lock); omp_set_lock(&tail_lock); tail = NULL; #pragma omp flush(tail) omp_unset_lock(&tail_lock); return data; } else{ int data = tail->data; NODE *ptr = head; while(ptr->next != tail) ptr = ptr->next; omp_set_lock(&tail_lock); tail = ptr; tail->next = NULL; #pragma omp flush(tail) omp_unset_lock(&tail_lock); return data; } }
int main (int argc, char *argv[]) { int nthreads, tid, i; float a[N], b[N]; omp_lock_t locka, lockb; /* Initialize the locks */ omp_init_lock(&locka); omp_init_lock(&lockb); /* Fork a team of threads giving them their own copies of variables */ #pragma omp parallel shared(a, b, nthreads, locka, lockb) private(tid) { /* Obtain thread number and number of threads */ tid = omp_get_thread_num(); #pragma omp master { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); } printf("Thread %d starting...\n", tid); #pragma omp barrier #pragma omp sections nowait { #pragma omp section { printf("Thread %d initializing a[]\n",tid); omp_set_lock(&locka); for (i=0; i<N; i++) a[i] = i * DELTA; omp_unset_lock(&locka);// if unset, the other thread will wait omp_set_lock(&lockb); printf("Thread %d adding a[] to b[]\n",tid); for (i=0; i<N; i++) b[i] += a[i]; omp_unset_lock(&lockb); //omp_unset_lock(&locka); } #pragma omp section { printf("Thread %d initializing b[]\n",tid); omp_set_lock(&lockb); for (i=0; i<N; i++) b[i] = i * PI; omp_unset_lock(&lockb);// if unset, the other thread will wait omp_set_lock(&locka); printf("Thread %d adding b[] to a[]\n",tid); for (i=0; i<N; i++) a[i] += b[i]; omp_unset_lock(&locka); //omp_unset_lock(&lockb); } } /* end of sections */ } /* end of parallel region */ }
void POMP2_Set_lock(omp_lock_t *s) { if ( pomp2_tracing ) { esd_enter(epk_omp_regid[EPK__OMP_SET_LOCK]); omp_set_lock(s); if ( pomp2_tracing > 1 ) esd_omp_alock(epk_lock_id(s)); esd_exit(epk_omp_regid[EPK__OMP_SET_LOCK]); } else { omp_set_lock(s); } }
void incrementer(omp_lock_t* lock_b) { for(int i=0; i< LOOP_ITS; ++i) { omp_set_lock(&g_lock_a); g_a++; omp_unset_lock(&g_lock_a); omp_set_lock(lock_b); g_b++; omp_unset_lock(lock_b); } }
void POMP_Set_lock(omp_lock_t *s) { if ( IS_POMP_TRACE_ON ) { uint64_t time = vt_pform_wtime(); vt_enter(&time, vt_omp_regid[VT__OMP_SET_LOCK]); omp_set_lock(s); time = vt_pform_wtime(); vt_omp_alock(&time, vt_lock_id(s)); vt_exit(&time); } else { omp_set_lock(s); } }
DEF_FPOMP_FUNC(void POMP_Set_lock_f(omp_lock_t *s)) { if ( IS_POMP_TRACE_ON ) { uint64_t time = vt_pform_wtime(); vt_enter(&time, vt_omp_regid[VT__OMP_SET_LOCK]); omp_set_lock(s); time = vt_pform_wtime(); vt_omp_alock(&time, vt_lock_id(s)); vt_exit(&time); } else { omp_set_lock(s); } } VT_GENERATE_F77_BINDINGS(pomp_set_lock, POMP_SET_LOCK,
VT_DECLDEF(void POMP_Set_lock_f(omp_lock_t *s)) { if ( IS_POMP_TRACE_ON ) { uint64_t time; time = vt_pform_wtime(); vt_enter(VT_CURRENT_THREAD, &time, vt_omp_regid[VT__OMP_SET_LOCK]); omp_set_lock(s); time = vt_pform_wtime(); vt_exit(VT_CURRENT_THREAD, &time); } else { omp_set_lock(s); } } VT_GENERATE_F77_BINDINGS(pomp_set_lock, POMP_SET_LOCK,
void POMP_Set_lock(omp_lock_t *s) { if ( IS_POMP_TRACE_ON ) { uint64_t time; time = vt_pform_wtime(); vt_enter(VT_CURRENT_THREAD, &time, vt_omp_regid[VT__OMP_SET_LOCK]); omp_set_lock(s); time = vt_pform_wtime(); vt_exit(VT_CURRENT_THREAD, &time); } else { omp_set_lock(s); } }
/* Looks up a previously stored state, or generates a new one. No assumptions are made about whether the board state is in reduced form already. Never fails. If the memory is full it allocates a new state regardless. If the state is found and returned it's OpenMP lock is first set. Thread-safe. RETURNS the state information */ tt_stats * tt_lookup_create( const board * b, bool is_black, u64 hash ){ u32 key = (u32)(hash % ((u64)number_of_buckets)); omp_lock_t * bucket_lock = is_black ? &b_table_lock : &w_table_lock; omp_set_lock(bucket_lock); tt_stats * ret = find_state(hash, b, is_black); if(ret == NULL) /* doesnt exist */ { if(states_in_use >= max_allocated_states) { /* It is possible in theory for a complex ko to produce a situation where freeing the game tree that is not reachable doesn't free any states. */ tt_log_status(); char * s = alloc(); board_to_string(s, b->p, b->last_played, b->last_eaten); flog_warn("tt", s); release(s); flog_warn("tt", "memory exceeded on root lookup"); } ret = create_state(hash); memcpy(ret->p, b->p, TOTAL_BOARD_SIZ); ret->last_eaten_passed = (b->last_played == PASS) ? PASS : b->last_eaten; omp_set_lock(&ret->lock); if(is_black) { ret->next = b_stats_table[key]; b_stats_table[key] = ret; } else { ret->next = w_stats_table[key]; w_stats_table[key] = ret; } omp_unset_lock(bucket_lock); } else /* update */ { omp_set_lock(&ret->lock); omp_unset_lock(bucket_lock); } return ret; }
/** * PRIVATE. Insert a region in a context. */ static inline int bfwork_region_insert(bam_fwork_t *fwork, bam_region_t *region) { linked_list_t *list; size_t list_l; assert(fwork); assert(region); omp_set_lock(&fwork->regions_lock); //List handle list = fwork->regions_list; list_l = linked_list_size(list); if(list_l >= FWORK_REGIONS_MAX) { omp_unset_lock(&fwork->regions_lock); //Wait for free slots if(!omp_test_lock(&fwork->free_slots)) { if(omp_get_num_threads() == 2) { #pragma omp taskwait //Force processing } omp_set_lock(&fwork->free_slots); } //LOG_FATAL_F("Not enough region slots, current: %d\n", list_l); } //This lock must be always locked until regions buffer have regions free omp_test_lock(&fwork->free_slots); //Add region to list linked_list_insert_last(region, list); omp_set_lock(®ion->lock); LOG_INFO_F("Inserting region %d:%lu-%lu with %d reads\n", region->chrom + 1, region->init_pos + 1, region->end_pos + 1, region->size); LOG_INFO_F("Regions to process %lu\n", linked_list_size(list)); omp_unset_lock(®ion->lock); omp_unset_lock(&fwork->regions_lock); return NO_ERROR; }
void *popBusyWait(struct Stack *s) { struct Node *temp; void *d; omp_set_lock(&s->lock); while (s->top == NULL) { omp_unset_lock(&s->lock); omp_set_lock(&s->lock); } d = s->top->data; temp = s->top; s->top = s->top->next; omp_unset_lock(&s->lock); free(temp); return d; }
void openmp_print_tid (void) { omp_set_lock (&writelock); SC_PRODUCTIONF ("Hello from thread %i.\n", omp_get_thread_num ()); omp_unset_lock (&writelock); }
OpenMPCounter& OpenMPCounter::operator ++() { omp_set_lock(&_lock); ++_counter; omp_unset_lock(&_lock); return *this; }
int ATL_DecAtomicCount(void *vp) { char *cp=vp; #ifdef ATL_OMP_THREADS omp_lock_t *mp; #else pthread_mutex_t *mp; #endif int *cntp; int iret; cntp = (int*)(cp+128); #ifdef ATL_OMP_THREADS mp = (omp_lock_t*)(cntp+2); omp_set_lock(mp); #else mp = (pthread_mutex_t*)(cntp+2); pthread_mutex_lock(mp); #endif iret = *cntp; if (iret) (*cntp)--; #ifdef ATL_OMP_THREADS omp_unset_lock(mp); #else pthread_mutex_unlock(mp); #endif return(iret); }
int main() { omp_lock_t lck; int id; omp_init_lock(&lck); #pragma omp parallel shared(lck) private(id) { id = omp_get_thread_num(); omp_set_lock(&lck); /* only one thread at a time can execute this printf */ printf("My thread id is %d.\n", id); omp_unset_lock(&lck); while (! omp_test_lock(&lck)) { skip(id); /* we do not yet have the lock, so we must do something else */ } work(id); /* we now have the lock and can do the work */ omp_unset_lock(&lck); } omp_destroy_lock(&lck); return 0; }
FLA_Error FLA_Axpy_sync_circular( FLA_Obj alpha, FLA_Obj X, FLA_Obj B ) { FLA_Obj XL, XR, X0, X1, X2; FLA_Obj BL, BR, B0, B1, B2; int n_stages = FLA_omp_get_num_stages(); int stage_width = FLA_omp_compute_stage_width( X ); int thread_num = omp_get_thread_num(); int n_done = 0; int b, i; // Start thread i on the ith panel partition of B. FLA_Part_1x2( X, &XL, &XR, stage_width*thread_num, FLA_LEFT ); FLA_Part_1x2( B, &BL, &BR, stage_width*thread_num, FLA_LEFT ); while ( n_done++ < n_stages ){ // The last lockable partition may be smaller than the others. b = min( FLA_Obj_width( XR ), stage_width ); FLA_Repart_1x2_to_1x3( XL, /**/ XR, &X0, /**/ &X1, &X2, b, FLA_RIGHT ); FLA_Repart_1x2_to_1x3( BL, /**/ BR, &B0, /**/ &B1, &B2, b, FLA_RIGHT ); /*------------------------------------------------------------*/ // Get the index of the current partition. i = FLA_Obj_width(XL)/stage_width; // Acquire lock[i] (the lock for X1 and B1). omp_set_lock( &fla_omp_lock[i] ); // B1 := alpha * X1 + B1 FLA_Axpy_external( alpha, X1, B1 ); // Release lock[i] (the lock for X1 and B1). omp_unset_lock( &fla_omp_lock[i] ); /*------------------------------------------------------------*/ FLA_Cont_with_1x3_to_1x2( &XL, /**/ &XR, X0, X1, /**/ X2, FLA_LEFT ); FLA_Cont_with_1x3_to_1x2( &BL, /**/ &BR, B0, B1, /**/ B2, FLA_LEFT ); // If this thread reaches the last partition, wrap back around to // the first partition for the next iteration. if( FLA_Obj_width( XL ) == FLA_Obj_width( X ) ) { FLA_Part_1x2( X, &XL, &XR, 0, FLA_LEFT ); FLA_Part_1x2( B, &BL, &BR, 0, FLA_LEFT ); } } return FLA_SUCCESS; }
/* Locks a single cell * i, j the coordinates of the cell; board, the current board */ void lockCell(int i, int j, Board board){ int width = board->width + 1; int offset = i * width + j; omp_set_lock(&(board->locks[offset])); }
void computeHistogram (const Eigen::MatrixXf &data, Eigen::MatrixXf &histogram, size_t bins, float min, float max) { float bin_size = (max-min) / bins; int num_dim = data.cols(); histogram = Eigen::MatrixXf::Zero (bins, num_dim); for (int dim = 0; dim < num_dim; dim++) { omp_lock_t bin_lock[bins]; for(size_t pos=0; pos<bins; pos++) omp_init_lock(&bin_lock[pos]); #pragma omp parallel for firstprivate(min, max, bins) schedule(dynamic) for (int j = 0; j<data.rows(); j++) { int pos = std::floor( (data(j,dim) - min) / bin_size); if(pos < 0) pos = 0; if(pos > (int)bins) pos = bins - 1; omp_set_lock(&bin_lock[pos]); histogram(pos,dim)++; omp_unset_lock(&bin_lock[pos]); } for(size_t pos=0; pos<bins; pos++) omp_destroy_lock(&bin_lock[pos]); } }
inline void CReferenceCounter::Release(void* pObject) { PNL_CHECK_IS_NULL_POINTER(pObject); omp_set_lock(&m_release_lock); std::list<void*>::iterator location = std::find( m_refList.begin(), m_refList.end(), pObject ); assert( location != m_refList.end() ); while( location != m_refList.end() ) { location = std::find( m_refList.erase(location), m_refList.end(), pObject ); } if( m_refList.empty() ) { omp_unset_lock(&m_release_lock); delete this; } else { omp_unset_lock(&m_release_lock); }; }
int colvarproxy_smp::smp_lock() { #if defined(_OPENMP) omp_set_lock(reinterpret_cast<omp_lock_t *>(omp_lock_state)); #endif return COLVARS_OK; }
static tt_stats * create_state( u64 hash ){ tt_stats * ret = NULL; omp_set_lock(&freed_nodes_lock); if(freed_nodes != NULL) { ret = freed_nodes; freed_nodes = freed_nodes->next; } else ++allocated_states; ++states_in_use; omp_unset_lock(&freed_nodes_lock); if(ret == NULL) { ret = (tt_stats *)malloc(sizeof(tt_stats)); if(ret == NULL) flog_crit("tt", "create_state: system out of memory"); omp_init_lock(&ret->lock); } /* careful that some fields are not initialized here */ ret->zobrist_hash = hash; ret->maintenance_mark = maintenance_mark; ret->plays_count = 0; ret->expansion_delay = expansion_delay; return ret; }
/*Each thread will call this function independantly. They calculate dataset *and then write it out to hdf, for NUM_MDSET times */ void CalcWriteData(hid_t fid, hid_t dataspace, hid_t datatype) { double data[NX][NY]; hid_t dataset; char dname[16]; int tid; int i, j, k; tid = omp_get_thread_num(); for(i=0; i<NUM_MDSET; i++) { /*Weird calculation to extend computing time*/ for(j=0; j<NX; j++) { for(k=0; k<NY; k++) data[j][k] = ( pow(sin(tid), 2.0) + pow(cos(tid), 2.0) ) * ( tid + i ) + ( pow(123456789.0, 8.0) - pow(123456789.0, 8.0) ); } sprintf(dname, "tid%d-dataset%d", tid, i); /*Serialize HDF dataset writing using OpenMP lock*/ omp_set_lock(&lock); dataset = H5Dcreate(fid, dname, datatype, dataspace, H5P_DEFAULT); H5Dwrite(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); H5Dclose(dataset); /*Release lock*/ omp_unset_lock(&lock); } }
int test_omp_lock() { int nr_threads_in_single = 0; int result = 0; int nr_iterations = 0; int i; omp_init_lock(&lck); #pragma omp parallel shared(lck) { #pragma omp for for(i = 0; i < LOOPCOUNT; i++) { omp_set_lock(&lck); #pragma omp flush nr_threads_in_single++; #pragma omp flush nr_iterations++; nr_threads_in_single--; result = result + nr_threads_in_single; omp_unset_lock(&lck); } } omp_destroy_lock(&lck); return ((result == 0) && (nr_iterations == LOOPCOUNT)); }
void ColorTransformOMP::initializeLUT() { omp_set_lock(&initialization_lock_); sRGB_LUT.resize(256); sXYZ_LUT.resize(4000); #pragma omp parallel for schedule (dynamic) for (int i = 0; i < 256; i++) { float f = static_cast<float> (i) / 255.0f; if (f > 0.04045) sRGB_LUT[i] = powf ((f + 0.055f) / 1.055f, 2.4f); else sRGB_LUT[i] = f / 12.92f; } #pragma omp parallel for schedule (dynamic) for (int i = 0; i < 4000; i++) { float f = static_cast<float> (i) / 4000.0f; if (f > 0.008856) sXYZ_LUT[i] = static_cast<float> (powf (f, 0.3333f)); else sXYZ_LUT[i] = static_cast<float>((7.787 * f) + (16.0 / 116.0)); } omp_unset_lock(&initialization_lock_); }
int CoutLock::set() { #ifdef _OPENMP omp_set_lock(&CoutLock::coutLock); #endif return 0; }