Пример #1
0
int main(){
    Timing timer;
#ifdef _DEBUG
    int size = 4;{
#else
    for(int size=1; size>0; size*=2){
#endif
    
        std::cout << "size: " << size << '\n';
        const int times = 10;
        Matrix a, b;
        a.resize(size);
        b.resize(size);
        a.randomize();
        b.randomize();

    
        timer.setDivisor(times);
    
   

        timer.start();
        for(int i=0; i<times; i++){
            Matrix c = MatrixMultiplication::recursive(a, b);
#ifdef _DEBUG
            c.print();
#endif
        }
        timer.end();
        timer.reportCPUtime();


        timer.start();
        for(int i=0; i<times; i++){
            Matrix c = MatrixMultiplication::strassen(a, b);
#ifdef _DEBUG
            c.print();
#endif
        }
        timer.end();
        timer.reportCPUtime();
    }

    return system("pause");
}
Пример #2
0
int main()
{
	/******************************* [ signal ] ******************************/
	Type a = 0;
	Type b = Ls-1;
	Vector<Type> t = linspace(a,b,Ls) / Type(Fs);
	Vector<Type> s = sin( Type(400*PI) * pow(t,Type(2.0)) );

	/******************************** [ widow ] ******************************/
	a = 0;
	b = Type(Lg-1);
	Type u = (Lg-1)/Type(2);
	Type r = Lg/Type(8);
	t = linspace(a,b,Lg);
	Vector<Type> g = gauss(t,u,r);
	g = g/norm(g);

	/********************************* [ WFT ] *******************************/
	Type runtime = 0;
	Timing cnt;
	cout << "Taking windowed Fourier transform." << endl;
	cnt.start();
    Matrix< complex<Type> > coefs = wft( s, g );
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	/******************************** [ IWFT ] *******************************/
	cout << "Taking inverse windowed Fourier transform." << endl;
	cnt.start();
	Vector<Type> x = iwft( coefs, g );
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	cout << "The relative error is : " << "norm(s-x) / norm(s) = "
		 << norm(s-x)/norm(s) << endl << endl;

	return 0;
}
Пример #3
0
int main()
{

	/******************************* [ signal ] ******************************/
	Vector<double> t = linspace( 0.0, (Ls-1)/fs, Ls );
	Vector<double> st = sin( 200*PI*pow(t,2.0) );
	st = st-mean(st);

	/******************************** [ CWT ] ********************************/
	Matrix< complex<double> > coefs;
	CWT<double> wavelet("morlet");
	wavelet.setScales( fs, fs/Ls, fs/2 );
	Timing cnt;
	double runtime = 0.0;
	cout << "Taking continuous wavelet transform(Morlet)." << endl;
	cnt.start();
	coefs = wavelet.cwtC(st);
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	/******************************** [ ICWT ] *******************************/
	cout << "Taking inverse continuous wavelet transform." << endl;
	cnt.start();
	Vector<double> xt = wavelet.icwtC(coefs);
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	cout << "The relative error is : " << endl;
	cout << "norm(st-xt) / norm(st) = " << norm(st-xt)/norm(st) << endl;
	cout << endl << endl;


    /******************************* [ signal ] ******************************/
    Vector<float> tf = linspace( float(0.0), (Ls-1)/float(fs), Ls );
	Vector<float> stf = sin( float(200*PI) * pow(tf,float(2.0) ) );
	stf = stf-mean(stf);

	/******************************** [ CWT ] ********************************/
	CWT<float> waveletf("mexiHat");
	waveletf.setScales( float(fs), float(fs/Ls), float(fs/2), float(0.25) );
	runtime = 0.0;
	cout << "Taking continuous wavelet transform(Mexican Hat)." << endl;
	cnt.start();
	Matrix<float> coefsf = waveletf.cwtR(stf);
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	/******************************** [ ICWT ] *******************************/
	cout << "Taking inverse continuous wavelet transform." << endl;
	cnt.start();
	Vector<float> xtf = waveletf.icwtR(coefsf);
	cnt.stop();
	runtime = cnt.read();
	cout << "The running time = " << runtime << " (ms)" << endl << endl;

	cout << "The relative error is : " << endl;
	cout << "norm(st-xt) / norm(st) = " << norm(stf-xtf)/norm(stf) << endl << endl;

	return 0;
}
 static inline Timing now() {
   Timing t;
   t.start();
   return t;
 }
int main(int argc, char* argv[]){

    // Initialize MPI
    // Find out my identity in the default communicator
    MPI_Init(&argc, &argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    //used to store the final results
    std::vector<result_t> finalResults;
    //init the k value
    int k = 0;

    if(argc > 1){
        try{
            k = atoi(argv[1]);
            if(k < 0){
                throw "just need to throw a random exception to catch`";
            }
        }
        catch(...){
            std::cerr<<"k must be a positive integer"<<std::endl;
            return 0;
        }

    }

    //init the custom openmpi datatype
    MPI_Datatype ResultMpiType;
    //wrapper function to set up the custom type
    createMPIResultStruct(&ResultMpiType);




    // Master
    if (rank == 0){
        // ++++++++++++++++++++++++++++++
        // Master process
        // ++++++++++++++++++++++++++++++

        // check the arugmumants
        if (argc < 2){
            std::cout << "Usage: " << argv[0] << " [k value] [directory path:default(/cluster)]" << std::endl;
            return 0;
        }

        std::string folderPath;

        //set the default for the folder to use
        if(argc == 2){
            folderPath = "/cluster";
        }
        else{
            folderPath = argv[2];
        }


        Directory dir;

        if(!dir.set_path(folderPath)){
            std::cerr<<"Directory either not found or not a directory"<<std::endl;
            return 0;
        }


        //test for a correct k value
        try{
            k = atoi(argv[1]);
            if(k < 0){
                throw "just need to throw a random exception to catch`";
            }
        }
        catch(...){
            std::cerr<<"k must be a positive integer"<<std::endl;
            return 0;
        }

        auto test = dir.get_files();
        searchVector_t searchVector;
        Timing wallClock;
        double timeResults[NUM_TIME_RESULTS];

        //initlize timing holder
        for(int i = 0; i < NUM_TIME_RESULTS; i++){
            timeResults[i] = 0;
        }

        wallClock.start();
        //get the first vector the first file in the the directory as the search vector
        if(!getFirstVector(test.at(0),&searchVector)){
            std::cerr << "Couldn't get the search vector" << std::endl;
            return 0;
        }
        std::cout << "Sending work to workers" << std::endl;
        sendWork(test,&ResultMpiType,k,&finalResults,searchVector.data,timeResults);



        //wait for the workers to finish to collect the results
        MPI_Barrier(MPI_COMM_WORLD);

        wallClock.end();
        std::cout<<"wallClock time: "<<wallClock.get_elapse()<<std::endl;

        //get com size for timming results
        int threadCount;
        MPI_Comm_size(MPI_COMM_WORLD, &threadCount);

        //creat a vector fromt the timing results
        std::vector<double> finalTimingVec;

        finalTimingVec.push_back(threadCount);
        finalTimingVec.push_back(k);
        finalTimingVec.push_back(wallClock.get_elapse());
        finalTimingVec.insert(finalTimingVec.end(),timeResults,timeResults+NUM_TIME_RESULTS);

        //output the timing vector to the timing for all the tests
        output_timing_vector_to_file("times.csv",finalTimingVec,1);

        //output the final results to a vector
        output_result_vector_to_file("results.csv", &finalResults);
    }
    else{
        // ++++++++++++++++++++++++++++++
        // Workers
        // ++++++++++++++++++++++++++++++

        doWork(&ResultMpiType,k);

        //used for the barrier in master
        MPI_Barrier(MPI_COMM_WORLD);
    }
    MPI_Type_free(&ResultMpiType);

    //Shut down MPI
    MPI_Finalize();

    return 1;

} // END of main
int doWork(MPI_Datatype* ResultMpiType,const int k){

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    int MAX_RESULT_SIZE = k;

    char msg[MAX_MSG_SIZE];
    float cmpData[SEARCH_VECTOR_SIZE];
    double times[2];
    MPI_Status status;
    Timing parserTime;
    Timing searchTime;
    Timing workWallTime;

    workWallTime.start();
    std::chrono::duration<double> read_time_elapse;

    MPI_Recv(cmpData,
        SEARCH_VECTOR_SIZE,
        MPI_FLOAT,
        0,
        MPI_ANY_TAG,
        MPI_COMM_WORLD,
        &status);

    if(status.MPI_TAG != SEARCH_VECTOR){
        std::cout<< rank << " recieved terminate signal" << std::endl;
            return 0;
    }


    while (1) {
        // Receive a message from the master
        MPI_Recv(msg,           /* message buffer */
            MAX_MSG_SIZE,     /* buffer size */
            MPI_CHAR,       /* data item is an integer */
            0,              /* Receive from master */
            MPI_ANY_TAG,
                MPI_COMM_WORLD,     /* default communicator */
                &status);


        // Check if we have been terminated by the master
        // exit from the worker loop
        if (status.MPI_TAG == TERMINATE) {
            std::cout<< rank << " recieved terminate signal" << std::endl;
            return 0;
        }


        std::shared_ptr<MapString_t> nameMap(new MapString_t);
        std::vector<result_t> results;
        std::shared_ptr<std::vector<float>> dataVector(new std::vector<float>);
        Parser p(nameMap,dataVector);

        parserTime.start();
        if(!p.parse_file(msg,&read_time_elapse)){
            std::cerr<<"could not parse file: "<<msg<<std::endl;
            return 0;
        }
        parserTime.end();
        searchTime.start();
        int lineLength = p.get_line_length();
        int index = 0;
        for(auto& file : *nameMap){
            results.push_back(result_t());
            results.at(index).distance = findDist(lineLength,
                dataVector,file.second,cmpData);
            strcpy(results.at(index).fileName,file.first.c_str());
            index++;
        }


        std::sort(results.begin(),results.end(),resultPairSort);


        results.resize(MAX_RESULT_SIZE);

        searchTime.end();

        MPI_Send(results.data(),           /* message buffer */
             MAX_RESULT_SIZE,         /* buffer size */
             *ResultMpiType,              /* data item is an integer */
             0,                     /* destination process rank, the master */
             RESULTS,             /* user chosen message tag */
             MPI_COMM_WORLD);

        workWallTime.end();

        //add the times to an array to be sent to the master
        times[0] = parserTime.get_elapse();
        times[1] = searchTime.get_elapse();
        times[2] = workWallTime.get_elapse();

        //send out the timing results to be compiled
        MPI_Send(times,
            NUM_TIME_RESULTS,
            MPI_DOUBLE,
            0,
            TIMING,
            MPI_COMM_WORLD);

    }







    return 0;

}
void main(void)
{
	int i, k;
	Timing watch;
	Point3D mid, ext;

	float cubeHalfSide = 100.0f; 
	float minSphereRadius; 
	float minHalfBoxExtent; 
	float maxSphereRadius;  
	float maxHalfBoxExtent; 

	// Note: Change the value of 'testCase' in [0, 4] to vary 
	// the number of overlaps in the generated test data.
	int testCase = 0;

	switch (testCase) {
		case 0:
			minSphereRadius = 1.0f;
			minHalfBoxExtent = 1.0f;
			maxSphereRadius = 32.5f;
			maxHalfBoxExtent = 32.5f;
			break;
		case 1:
			minSphereRadius = 1.0f;
			minHalfBoxExtent = 1.0f;			
			maxSphereRadius = 55.0f;
			maxHalfBoxExtent = 55.0f;
			break;
		case 2:
			minSphereRadius = 1.0f;
			minHalfBoxExtent = 1.0f;
			maxSphereRadius = 77.5f;
			maxHalfBoxExtent = 77.5f;
			break;
		case 3:
			minSphereRadius = 13.5f;
			minHalfBoxExtent = 13.5f;
			maxSphereRadius = 85.0f;
			maxHalfBoxExtent = 85.0f;
			break;
		case 4:
			minSphereRadius = 28.0f;
			minHalfBoxExtent = 28.0f;
			maxSphereRadius = 99.0f;
			maxHalfBoxExtent = 99.0f;
			break;
		default:
			minSphereRadius = 1.0f;
			minHalfBoxExtent = 1.0f;
			maxSphereRadius = 10.0f;
			maxHalfBoxExtent = 10.0f;
	}

	for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
		sphereArray[i].r = getRandomScalar(minSphereRadius, maxSphereRadius);
		
		sphereArray[i].c.x = getRandomScalar(-cubeHalfSide + sphereArray[i].r, cubeHalfSide - sphereArray[i].r);
		sphereArray[i].c.y = getRandomScalar(-cubeHalfSide + sphereArray[i].r, cubeHalfSide - sphereArray[i].r);
		sphereArray[i].c.z = getRandomScalar(-cubeHalfSide + sphereArray[i].r, cubeHalfSide - sphereArray[i].r);
		
		ext.x = getRandomScalar(minHalfBoxExtent, maxHalfBoxExtent);
		ext.y = getRandomScalar(minHalfBoxExtent, maxHalfBoxExtent);
		ext.z = getRandomScalar(minHalfBoxExtent, maxHalfBoxExtent);
		
		mid.x = getRandomScalar(-cubeHalfSide + ext.x, cubeHalfSide - ext.x);
		mid.y = getRandomScalar(-cubeHalfSide + ext.y, cubeHalfSide - ext.y);
		mid.z = getRandomScalar(-cubeHalfSide + ext.z, cubeHalfSide - ext.z);

		boxArray[i].min.x = mid.x - ext.x;
		boxArray[i].min.y = mid.y - ext.y;
		boxArray[i].min.z = mid.z - ext.z;
		boxArray[i].max.x = mid.x + ext.x;
		boxArray[i].max.y = mid.y + ext.y;
		boxArray[i].max.z = mid.z + ext.z;
	}

	/* ==== Method 1 ==== */
	noOverlaps[0] = 0;
	watch.start();
	for (k=0; k < REPETITIONS; k++) {		
		for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
			noOverlaps[0] += overlapSphereAABB_Arvo(sphereArray[i], boxArray[i]);
		}
	}
	time[0] = watch.stop() / REPETITIONS;
	noOverlaps[0] /= REPETITIONS;
	printResult(algorithmName[0], noOverlaps[0], time[0]);

	/* ==== Method 2 ==== */
	noOverlaps[1] = 0;
	watch.start();
	for (k=0; k < REPETITIONS; k++) {		
		for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
			noOverlaps[1] += overlapSphereAABB_QRI(sphereArray[i], boxArray[i]);
		}
	}
	time[1] = watch.stop() / REPETITIONS;
	noOverlaps[1] /= REPETITIONS;
	printResult(algorithmName[1], noOverlaps[1], time[1]);

	/* ==== Method 3 ==== */
	noOverlaps[2] = 0;
	watch.start();
	for (k=0; k < REPETITIONS; k++) {		
		for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
			noOverlaps[2] += overlapSphereAABB_QRF(sphereArray[i], boxArray[i]);
		}
	}
	time[2] = watch.stop() / REPETITIONS;
	noOverlaps[2] /= REPETITIONS;
	printResult(algorithmName[2], noOverlaps[2], time[2]);

	/* ==== Method 4 ==== */
	noOverlaps[3] = 0;
	watch.start();
	for (k=0; k < REPETITIONS; k++) {		
		for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
			noOverlaps[3] += overlapSphereAABB_Cons(sphereArray[i], boxArray[i]);
		}
	}
	time[3] = watch.stop() / REPETITIONS;
	noOverlaps[3] /= REPETITIONS;
	printResult(algorithmName[3], noOverlaps[3], time[3]);

	/* ==== Method 5 ==== */
	noOverlaps[4] = 0;
	watch.start();
	for (k=0; k < REPETITIONS; k++) {		
		for (i = 0; i < NUMBER_OF_BV_PAIRS; i++) {
			noOverlaps[4] += overlapSphereAABB_SSE(sphereArray[i], boxArray[i]);
		}
	}
	time[4] = watch.stop() / REPETITIONS;
	noOverlaps[4] /= REPETITIONS;
	printResult(algorithmName[4], noOverlaps[4], time[4]);

	/* ==== Result Summary ==== */
	printf("=== Result summary ===\n\n");
	
	printf("Number of overlap tests: %d\n\n", NUMBER_OF_BV_PAIRS);

	printf("Algorithm\t\tTime\t(Speedup)\n");
	printf("%s\t%.4f\n", algorithmName[0], time[0]);
	for (i = 1; i < NUMBER_OF_METHODS; i++) {
		printf("%s\t%.4f\t(%.3lf)\n", algorithmName[i], time[i], time[0] / time[i]);
	}
	printf("\n");

	printf("Overlaps: %.2f percent\n", 100.0f * (float)noOverlaps[0] / NUMBER_OF_BV_PAIRS);	
	int noOverlapsReal = noOverlaps[0];
	int noOverlapsConservative = noOverlaps[3];
	printf("False positives reported in conservative test: %.3f percent\n", 100.0f * (noOverlapsConservative - noOverlapsReal) / (float)noOverlapsReal);
	getchar();
}