Esempio n. 1
0
static
void buildSorter(vec<Formula>& ps, vec<int>& Cs, vec<Formula>& out_sorter)
{
    out_sorter.clear();
    for (int i = 0; i < ps.size(); i++)
        for (int j = 0; j < Cs[i]; j++)
            out_sorter.push(ps[i]);
    oddEvenSort(out_sorter); // (overwrites inputs)
}
static
void buildSorter(vec<Formula>& ps, vec<int>& Cs, vec<Formula>& out_sorter, PBOptions* options)
{
    out_sorter.clear();
    for (int i = 0; i < ps.size(); i++)
        for (int j = 0; j < Cs[i]; j++)
            out_sorter.push(ps[i]);
    if (options->opt_sorting_network_encoding==pairwiseSortEncoding)
    	pw_sort(out_sorter);
    else
    	oddEvenSort(out_sorter); // (overwrites inputs)
}
Esempio n. 3
0
/**
 * This function creates quadratic matrix which fits into given comm.
 * It creates cartesian comm of same size as the matrix.
 * @param MPI_Comm comm Communicator to work in.
 */
void sort(MPI_Comm comm)
{
	int me,
		master = 0,
		commSize,
		dims[2],
		coords[2],
		myNum,
		periods[] = {0, 0},
		*numbers = 0, // Initialized to avoid warnigns
		size;
		
	MPI_Comm cartComm;
	
	// Create quadratic matrix comm which fits in given comm.
	MPI_Comm_size(comm, &commSize);
	
	size = (int)sqrt((float)commSize);
	
	dims[0] = dims[1] = size;
	
	MPI_Cart_create(comm, 2, dims, periods, 0, &cartComm);
	
	if(cartComm == MPI_COMM_NULL)
	{
		return;
	}
	
	MPI_Comm_rank(cartComm, &me);
	
	// If master allocate space, generate array and print it
	if(me == master)
	{
		numbers = (int*)malloc( (int)pow((float)size, 2) * sizeof(int) );
		generateRandomIntArray(numbers, (int)pow((float)size, 2), maxRandom);
		printf("Initial matrix:\n");
		printIntMatrix(numbers, size, size);
	}
	
	// Send the numbers to everyone
	MPI_Scatter(numbers, 1, MPI_INT, &myNum, 1, MPI_INT, master, cartComm);
	
	// Get my coords
	MPI_Cart_coords(cartComm, me, 2, coords);
	
	for(int i = 0; i < log((float)size) / log(2.) + 1; i++)
	{
		// Row sort step
		if(coords[0] % 2 == 0)
		{
			myNum = oddEvenSort(cartComm, size, myNum, 1, 0);
		}
		else
		{
			myNum = oddEvenSort(cartComm, size, myNum, 1, 1);
		}
		
		// Column sort step
		myNum = oddEvenSort(cartComm, size, myNum, 0, 0);
	}
	
	// Get results
	MPI_Gather(&myNum, 1, MPI_INT, numbers, 1, MPI_INT, master, cartComm);
	
	// If master print results and free allocated memory
	if(me == master)
	{
		printf("Final matrix:\n");
		printIntMatrix(numbers, size, size);
		free(numbers);
	}
}
Esempio n. 4
0
//Main Method
int main(int argc, char *argv[1]){

	//Local Declarations
	int i = 0;
	int *b;
	int size;
	int j;
	int key;
	FILE* file = fopen(argv[1], "r");

	//Statements
		//File Checker
	        if(file == NULL){
		printf("Data not found. Please run the program again with the data.");
		exit(1);
		}//end if 

		//Read a file
		fscanf(file, "%d", &size);
   		//printf("\nHere is the size of the array: %d \n",size);
 		
		//Calloc file
		b = (int*)calloc(size, sizeof(int));
		
	
		printf("\nHere is the data from the data file\n");
   		for(j=0; j<size; j++){
    			fscanf(file, "%d ", &b[j]);
   			//printf("%d ", b[j]);
  		 }
		

		//Sorted the array
		oddEvenSort(b, size);
		printf("The array is already sorted. \n");
	/*	for(j=0; j<size; j++){
			printf("%d ", b[j]);	
		}*/		
		
		//Get an input from the user
		while(!(key == -1)){
		printf("Please enter a number to find its index (-1 ends the program): ");
		scanf("%d", &key);
		if(!(key == -1)){		
			if(key > size-1){
				printf("The number entered is too large. Please enter an index less than or equal %d \n", size-1);
				continue;
			}//end if
			else if(key < -1){
				printf("The entered is below the array index. Please enter a number larger than -2 \n");
				continue;
			}//end else if
			else
			printf("The number found at index %d is: %d \n", key, b[key]);
			}//end if 
		}//end while

		fclose(file);
		free(b);
				
}//end main