BitHandle BitField::get_handle() { // find first free bit // first find first free word unsigned int word; for (word = 0 ; word < f_num_words ; word++ ) if ( word_has_zero_bit(f_bits_in_use[word]) ) break; if (word == f_num_words) { check_allocation (word); #if 0 // have to allocate more unsigned long *new_words = new unsigned long [f_num_words + 1] ; unsigned long *new_use = new unsigned long [f_num_words + 1] ; for (int i = 0 ; i < f_num_words ; i++ ){ // copy old info new_words[i] = f_words[i] ; new_use[i] = f_bits_in_use[i] ; } // zero out newest memory new_words[f_num_words] = 0 ; new_use[f_num_words] = 0 ; delete f_words ; f_words = new_words ; delete f_bits_in_use ; f_bits_in_use = new_use ; f_num_words++ ; #endif return get_handle(); // NOTE: recursive and function exit } // now find first free byte in word int byte; for (byte = 0 ; byte < bytes_per_word ; byte++ ) if (byte_has_zero_bit(get_byte(byte, f_bits_in_use[word]))) break; unsigned char the_byte = (unsigned char) (f_bits_in_use[word] >> (byte * bits_per_byte)) & 0xFF ; // now check byte for zero bit int bit; for (bit = 0 ; bit < bits_per_byte ; bit++ ) if ((the_byte & mask[bit]) == 0) break ; // mark the bit as in use f_bits_in_use[word] |= 1 << ((byte * bits_per_byte) + bit) ; return (word * bits_per_word) + (byte * bits_per_byte) + bit ; }
int allocate_send_lists(int myrank, int *nghb_cnt, int** nghb_to_rank, int** send_cnt, int*** send_lst, int **recv_cnt) { MPI_Status status; int nghb_idx=0; // Allocate send_cnt *send_cnt = (int*) calloc(sizeof(int), *nghb_cnt); // Send size of receive list for that processor, it will be equal to the size of send list of // received processor for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { // Send sizes MPI_Send(&(*recv_cnt)[nghb_idx], 1, MPI_INT, (*nghb_to_rank)[nghb_idx], myrank, MPI_COMM_WORLD); // Receive sizes MPI_Recv(&(*send_cnt)[nghb_idx],1 , MPI_INT, (*nghb_to_rank)[nghb_idx], (*nghb_to_rank)[nghb_idx], MPI_COMM_WORLD, &status); } // Allocate send_lst with given sizes *send_lst = (int**) malloc((*nghb_cnt)*sizeof(int*)); check_allocation(myrank, send_lst, "malloc of send_lst failed"); for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { (*send_lst)[nghb_idx] = (int *) malloc((*send_cnt)[nghb_idx]*sizeof(int)); } return POSL_OK; }
struct lsh_iterator* lsh_initialize(int len1, int len2, int(*penalty_)(int, int), int bandwidth, int *result) { struct lsh_iterator* ret; assert (!busy); busy = 1; n1 = len1 + 1, n2 = len2 + 1; /* this is ugly comunication between lsh_initialize_mcmp and lsh_initialize * to check if we have mpenalty */ if (bandwidth < 0) { bandwidth *= -1; } else { mpenalty = NULL; mmpenalty = NULL; } penalty = penalty_; K = bandwidth; adjust_bandwidth(); double_K_plus_1 = 2*K + 1; check_allocation(); edit_distance(); output(); if (result != NULL) { *result = dp_read[n2 - n1 + K]; } ret = (struct lsh_iterator*) malloc(sizeof(struct lsh_iterator)); ret->_row = n1 - 1; ret->_col = n2 - n1 + K; return ret; }
void initiate_inode_list() { int arr[512]; memset(arr,0 ,sizeof(arr)); int i; uint max_inode_block = 1 + curr_superblock.isize; for(i = 2; i <= max_inode_block; i++) write_block(i, arr, sizeof(arr)); //int brr[512]; //read_block(20,brr,512); uint inode_number = curr_superblock.isize * INODES_PER_BLOCK; for(i = 2; i <= inode_number; i++) { if(curr_superblock.ninode == MAX_SIZE) break; struct inode ino; read_inode(i, &ino); if(check_allocation(&ino) != 1) { free_inode(i); } } if(curr_superblock.ninode == 0){ fprintf(stderr,"Error: the inode blocks are full, allocation falure!"); exit(-1); } }
int allocate_lcc_elems_points(int read_key, int myrank, int nprocs, int *nintci, int *nintcf, int *nextci, int ***lcc, int* points_count, int*** points, int** elems, int **local_global_index, int points_count_g, int *int_cells_per_proc) { int i=0; MPI_Status status; //starting index is same for all processors *nintci = 0; if (read_key == POSL_INIT_ONE_READ) { if (myrank == 0) { *points_count = points_count_g; for (i=1; i<nprocs; i++) { MPI_Send(&int_cells_per_proc[i], 1, MPI_INT, i, POSL_MPI_TAG_NINTCF, MPI_COMM_WORLD); MPI_Send(&points_count_g, 1, MPI_INT, i, POSL_MPI_TAG_POINTS_COUNT, MPI_COMM_WORLD); } } else { MPI_Recv(nintcf, 1, MPI_INT, 0, POSL_MPI_TAG_NINTCF, MPI_COMM_WORLD, &status); MPI_Recv(points_count, 1, MPI_INT, 0, POSL_MPI_TAG_POINTS_COUNT, MPI_COMM_WORLD, &status); //decrement the value, since our indexing starts from zero *nextci = *nintcf; --(*nintcf); } } else { *points_count = points_count_g; } *lcc = (int**) malloc(((*nintcf)+1)*sizeof(int*)); check_allocation(myrank, lcc, "Failed to allocate first dimension of LCC"); for (i=0; i<(*nintcf)+1; i++) { (*lcc)[i] = (int *) malloc(6*sizeof(int)); check_allocation(myrank, lcc+i, "malloc failed to allocate second dimension of lcc"); } *elems = (int *) malloc(((*nintcf)+1)*8*sizeof(int)); check_allocation(myrank, elems, "Failed to allocate memory for elems"); *points = (int **) calloc(*points_count, sizeof(int*)); check_allocation(myrank, elems, "Failed to allocate memory for points 1st dimention"); for (i=0; i<*points_count; i++) { (*points)[i] = (int *) calloc(3, sizeof(int)); check_allocation(myrank, points+i, "malloc() POINTS 2nd dim. failed"); } return POSL_OK; }
int fill_l2g(int read_key, int myrank, int nproc, int nintcf, int** local_global_index, int ***local_global_index_g, int *partitioning_map, int nelems_g, int *int_cells_per_proc) { int i=0, local_idx=0, current_proc=0; *local_global_index = (int *) malloc(((nintcf)+1)*sizeof(int)); check_allocation(myrank, local_global_index, "Malloc for local_global_index failed"); for (i=0; i<nelems_g; ++i) { if (partitioning_map[i] == myrank) { (*local_global_index)[local_idx] = i; ++local_idx; } } if (read_key == POSL_INIT_ONE_READ && myrank == 0) { *local_global_index_g = (int**) malloc(nproc*sizeof(int*)); check_allocation(myrank, local_global_index_g, "malloc failed to allocate first dimension of l2g_g"); for (i=0; i<nproc; i++) { (*local_global_index_g)[i] = (int*) malloc(int_cells_per_proc[i]*sizeof(int)); check_allocation(myrank, (local_global_index_g+i), "malloc failed to allocate 2nd dimension of l2g_g"); } //initialize array of local indexes int last_local_idx[nproc]; for (i=0; i<nproc; ++i) { last_local_idx[i] = 0; } for (i=0; i<nelems_g; ++i) { current_proc = partitioning_map[i]; local_idx = last_local_idx[current_proc]; if (local_idx > int_cells_per_proc[current_proc]) { log_err("Wrong local index for processor #%d", current_proc); return POSL_ERROR; } (*local_global_index_g)[current_proc][local_idx] = i; ++(last_local_idx[current_proc]); } } return POSL_OK; }
bool BitField::i_is_set(BitHandle handle) { assert(handle); unsigned long word = handle / bits_per_word ; check_allocation (word); unsigned long mask = 1 << (handle % bits_per_word) ; return (f_words[word] & mask) != 0 ; }
bool BitField::i_unset(BitHandle handle) { assert(handle); unsigned long word = handle / bits_per_word ; check_allocation (word); unsigned long mask = 1 << (handle % bits_per_word) ; f_words[word] &= ~0L ^ mask ; return 0 ; }
bool BitField::i_set(BitHandle handle) { assert(handle); unsigned long word = handle / bits_per_word ; check_allocation (word); unsigned long mask = 1 << (handle % bits_per_word) ; ON_DEBUG (printf ("BitField: setting bit %ld (in word %ld)\n", handle, word)); f_words[word] |= mask ; return 1 ; }
int partition(int part_key, int read_key, int myrank, int nprocs, int nintci_g, int nintcf_g, int nextci_g, int nextcf_g, int *nintci, int *nintcf, int *nextci, int *nextcf, int **lcc_g, int points_count_g, int **points_g, int *elems_g, int *int_cells_per_proc, int *extcell_per_proc, int **local_global_index_g, int **local_global_index, int **partitioning_map) { int i=0; idx_t nelems, nnodes, ncommon=4, nparts, objval; idx_t *elem_ptr, *elem_idx, *elem_part, *node_part; nelems = nintcf_g-nintci_g+1; *partitioning_map = (int *) calloc(sizeof(int), (nintcf_g-nintci_g+1)); check_allocation(myrank, partitioning_map, "Partitioning map allocation failed"); if (((read_key == POSL_INIT_ONE_READ) && (myrank == 0)) || (read_key == POSL_INIT_ALL_READ)) { *nintci = 0; *nintcf = 0; if (part_key == POSL_PARTITIONING_CLASSIC) { //the last processor always gets different number of cells int elem_per_proc = (nelems+(nprocs-1))/nprocs; *nextci = (myrank == nprocs-1) ? nelems-(nprocs-1)*elem_per_proc : elem_per_proc; *nintcf = *nextci-1; //build global cell allocation if (read_key == POSL_INIT_ONE_READ) { for (i=0; i<(nprocs-1); ++i) { int_cells_per_proc[i] = elem_per_proc; } int_cells_per_proc[nprocs-1] = nelems-(nprocs-1)*elem_per_proc; } for (i=0; i<nelems; ++i) { (*partitioning_map)[i] = i/elem_per_proc; } } else { //initialize variables for metis nnodes = points_count_g; nparts = nprocs; elem_ptr = (idx_t *) calloc(nelems+1, sizeof(idx_t)); elem_idx = (idx_t *) calloc(nelems*8, sizeof(idx_t)); elem_part = (idx_t *) calloc(nelems, sizeof(idx_t)); node_part = (idx_t *) calloc(nnodes, sizeof(idx_t)); //assign arrays that store metis graph mesh for (i=0; i<(nelems+1); i++) { elem_ptr[i] = 8*i; } for (i=0; i<(nelems*8); i++) { elem_idx[i] = elems_g[i]; } //perform metis partitioning if (part_key == POSL_PARTITIONING_DUAL) { METIS_PartMeshDual(&nelems, &nnodes, elem_ptr, elem_idx, NULL, NULL, &ncommon, &nparts, NULL, NULL, &objval, elem_part, node_part); } else { METIS_PartMeshNodal(&nelems, &nnodes, elem_ptr, elem_idx, NULL, NULL, &nparts, NULL, NULL, &objval, elem_part, node_part); } for (i=0; i<nelems; i++) { (*partitioning_map)[i] = (int) elem_part[i]; } //initialize global cell counters if (read_key == POSL_INIT_ONE_READ) { for (i=0; i<nprocs; ++i) { int_cells_per_proc[i] = 0; } } //TODO: consider performance gains when if statement is outside of the loop //compute position of last internal cell for (i=0; i<nelems; i++) { if (read_key == POSL_INIT_ONE_READ) { int_cells_per_proc[(*partitioning_map)[i]] += 1; } else { if (myrank == (*partitioning_map)[i]) { (*nintcf) += 1; } } } //assign local internal cell ending idx if (read_key == POSL_INIT_ONE_READ) { *nintcf = int_cells_per_proc[myrank]; } *nextci = (*nintcf)--; } } return POSL_OK; }
int build_lists_g2l_next(int nprocs, int myrank, int *partitioning_map, int nintcf_g, int nextcf_g, int* nintcf, int* nextcf, int*** lcc, int** local_global_index, int** global_local_index, int *nghb_cnt, int** nghb_to_rank, int **recv_cnt, int*** recv_lst) { /*********** Initialize and allocate g2l and tmp variables ************************************/ int proc=0, i=0, j=0, is_ghost_cell=0, idx_g=-1, neighbor_rank=-1; int n_ghost_cells[nprocs], start_idx_per_proc[nprocs]; int nghb_idx=0; memset(n_ghost_cells, 0, nprocs*sizeof(int)); memset(start_idx_per_proc, 0, nprocs*sizeof(int)); *nextcf = 0; // Used to check if we already saved the cel index int is_saved[nextcf_g+1]; /** Lists for neighboring information */ /// number of cells to be received from each neighbor (size: nprocs) int tmp_recv_cnt[nprocs]; memset(tmp_recv_cnt, 0, nprocs*sizeof(int)); // TODO: try not to use tmp_recv_lst and save all data immediately in revc_lst // lists of cells to be received from each neighbor (size: nprocs x recv_cnt[*]) int *tmp_recv_lst[nprocs]; *global_local_index = (int *) malloc((nextcf_g+1)*sizeof(int)); check_allocation(myrank, global_local_index, "malloc(global_local_index) failed"); // We will fill whole g2l by -1 for easier debugging for (i=0; i<=nextcf_g; ++i) { (*global_local_index)[i] = -1; } /*********** End initialize and allocate g2l and tmp variables ********************************/ // TODO: We can do all work in one big loop but then we need to use the buffer before we allocate // memory for tmp_recv_lst /************************ Start processing lcc and generating data ****************************/ memset(is_saved, 0, (nextcf_g+1)*sizeof(int)); // Count number of external cells and fill g2l with external cells, and fill tmp_recv_cnt for (i=0; i<=(*nintcf); ++i) { for (j=0; j<6; ++j) { idx_g = (*lcc)[i][j]; if(idx_g<=nintcf_g) { neighbor_rank = partitioning_map[idx_g]; is_ghost_cell = neighbor_rank != myrank; } if (!is_saved[idx_g]) { if((*lcc)[i][j] > nintcf_g) { ++(*nextcf); is_saved[idx_g] = 1; (*global_local_index)[idx_g] = (*nintcf) + (*nextcf); } else if (is_ghost_cell) { ++tmp_recv_cnt[neighbor_rank]; is_saved[idx_g] = 1; } } } // Fill g2l with internal cell (*global_local_index)[(*local_global_index)[i]] = i; } /// After the loop nextcf shows amount of elements *nextcf = *nintcf + *nextcf; // Allocate tmp_recv_lst for (proc=0; proc<nprocs; ++proc) { if(tmp_recv_cnt[proc] == 0) { tmp_recv_lst[proc] = NULL; } else { tmp_recv_lst[proc] = (int *) calloc(sizeof(int), tmp_recv_cnt[proc]); } } // Fill tmp_recv_list and g2l with ghost cells start_idx_per_proc[0]=0; for(proc=1; proc<nprocs; ++proc) { start_idx_per_proc[proc] = start_idx_per_proc[proc-1] + tmp_recv_cnt[proc-1]; } memset(is_saved, 0, (nextcf_g+1)*sizeof(int)); for (i=0; i<=(*nintcf); ++i) { for (j=0; j<6; ++j) { idx_g = (*lcc)[i][j]; if(idx_g<=nintcf_g) { neighbor_rank = partitioning_map[idx_g]; is_ghost_cell = neighbor_rank != myrank; } else { is_ghost_cell = 0; } if (is_ghost_cell && !is_saved[idx_g]) { is_saved[idx_g] = 1; tmp_recv_lst[neighbor_rank][n_ghost_cells[neighbor_rank]] = idx_g; ++n_ghost_cells[neighbor_rank]; (*global_local_index)[idx_g] = (*nextcf) + start_idx_per_proc[neighbor_rank] + n_ghost_cells[neighbor_rank]; } } } // Count neighbors it is already set to zero in gccg.c for (proc=0; proc<nprocs; ++proc) { if (tmp_recv_cnt[proc] !=0) ++(*nghb_cnt); } // Allocate and fill nhb_to_rank *nghb_to_rank = (int *) malloc((*nghb_cnt)*sizeof(int)); check_allocation(myrank, nghb_to_rank, "malloc(nghb_to_rank) failed"); for (proc=0;proc<nprocs; ++proc) { if(tmp_recv_cnt[proc] !=0) { (*nghb_to_rank)[nghb_idx] = proc; ++nghb_idx; } } // Allocate and fill recv_cnt and recv_lst with global lcc indexing for further use *recv_cnt = (int*) calloc(sizeof(int), *nghb_cnt); for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { (*recv_cnt)[nghb_idx] = tmp_recv_cnt[ (*nghb_to_rank)[nghb_idx] ]; } *recv_lst = (int**) malloc( (*nghb_cnt)*sizeof(int*) ); for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { (*recv_lst)[nghb_idx] = (int *) malloc( (*recv_cnt)[nghb_idx] * sizeof(int) ); } for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { memcpy( (*recv_lst)[nghb_idx], tmp_recv_lst[ (*nghb_to_rank)[nghb_idx] ], (*recv_cnt)[nghb_idx]*sizeof(int) ); } // Total number of external cells for (nghb_idx=0; nghb_idx<(*nghb_cnt); ++nghb_idx) { *nextcf += (*recv_cnt)[nghb_idx]; } /************************ End processing lcc and generating data ******************************/ // Free memory for (proc=0; proc<nprocs; ++proc) { free(tmp_recv_lst[proc]); } return POSL_OK; }