Esempio n. 1
0
int main (int argc, char *argv[]) {

  double tis;
  u32 size = atoi(argv[1]);
  vectoru32 v;
  generateRandomVector(size, v);
  
#ifdef DEBUG
  std::cout << "Unsorted vector\n";
  printVector(v);
#endif

  tis = wtime();
  insertionSort(v);
  //insertionSortRec(v, v.size());
  tis = wtime() - tis;

#ifdef DEBUG
  std::cout << "\nSorted vector\n";
  printVector(v);
#endif

  //printf("[insertion sort] Size of vector : %lu. Elapsed time: %.6f sec.\n", v.size(), tis);
  printf("%lu %.6f\n", v.size(), tis);  
  return 0;
}
int main (int argc, char *argv[]) {

  double tis;
  u32 size = atoi(argv[1]);
  vectoru32 v;
  //srand(wtime());
  generateRandomVector(size, v);

#ifdef DEBUG
  std::cout << "Unsorted vector\n";
  printVector(v);
#endif

  tis = wtime();
  randomizedQuickSort(v, 0, v.size() - 1);
  tis = wtime() - tis;

#ifdef DEBUG
  std::cout << "\nSorted vector\n";
  printVector(v);
#endif

  printf("%lu %.6f\n", v.size(), tis);  
  return 0;
}
Esempio n. 3
0
int main( int argc, char **argv ){
   
   Stopwatch timer;


   // For each circular search vector size
   for(int l=0; l < 4; l++){
      std::cout << br << "\nCircular Search Size: " << vectorSize[l] << "\n";

      // Big V
      for(size_t v = 0; v < BIG_V_SIZE; v++){
         std::cout << "V[" << v << "] - circularSize: " << LITTLE_V_SIZE + vectorSize[l] << "\n";
         V[v] = generateRandomVector(BIG_V_SIZE);
         
         
         pid_t pid;

         int g = 0;

         pid_t pID = fork();
         if (pID < 0) { std::cerr << "Failed to fork" << std::endl;exit(1);}

         if (pID == 0) { //child

            g = 0;
            std::cout << "Child Process" << g << std::endl;

            
         } else { // parent

            std::cout << "Parent Process:" << g << std::endl;;
            ++g;
         }

         // little v 
			for(int i = 0; i < LITTLE_V_SIZE + vectorSize[l]; i++){

            // std::cout << i << ":" << i%LITTLE_V_SIZE << ":" << V[v][i%LITTLE_V_SIZE] << ", ";

            TV[i%vectorSize[l]] = V[v][i%LITTLE_V_SIZE];
			}
      }
   }


	 timer.stop();
    std::cout << "\n\noperation took " << timer.total() << " seconds\n";
	 return 0;
}
Esempio n. 4
0
int main (int argc, char** argv) {
    if(argc < 4) {
        std::cout << "3 command line arguments are needed.\nPlease execute with ./hw0 <input_filename> <process #> <number of match list>\n";
        return 0;
    }
    FILE* fPtr; //file pointer to file to be parsed
    char *line; //line to parse file line by line

    fPtr = fopen(argv[1], "r");
    int total_rows = 0;
    line = (char*)malloc(sizeof(char)*LINE_MAX);
    VectorsMap points;
    Parser fileParser;
    //vectors that will be generated for the runs.
    std::vector<std::vector<float>> generated_vectors(30);

    //parse the file line by line
    while(fgets(line, LINE_MAX, fPtr)) {
        //make sure that we do not read an empty line
        if(line[0] != '\0') {
            //send the line to be further parsed
            points.push_back(fileParser.parseLine(line));
            //increment total rows count
            total_rows++;
        }
    }
    free(line);
    fclose(fPtr);

    srand(34122);
    int i,j, process_count=atoi(argv[2]), num_max=atoi(argv[3]), size=0;
    //the size of the search vectors
    int sizes[] = {9,11,17,29};
    int sizes_count = 4;
    //will hold the offsets for each process
    std::vector<segment> segments(process_count);

    //initialize shared memory
    size_t memory_space = process_count*4*num_max;
    float shm_size = memory_space * sizeof(float);
    int shmId;
    // use current time as seed for random generator
    std::srand(std::time(0));
    key_t shmKey = std::rand();
    int shmFlag = IPC_CREAT | 0666;
    float * shm;

    /* Initialize shared memory */
    if((shmId = shmget(shmKey, shm_size, shmFlag)) < 0)
    {
        std::cerr << "Init: Failed to initialize shared memory (" << shmId << ")" << std::endl;
        exit(1);
    }

    if((shm = (float *)shmat(shmId, NULL, 0)) == (float *) -1)
    {
        std::cerr << "Init: Failed to attach shared memory (" << shmId << ")" << std::endl;
        exit(1);
    }
    //get number of items for each process in shared memory
    const unsigned int per_procc_mem = num_max*4;
    //initialize offsets
    for(i=0; i< process_count; i++) {
        //get segments for dataset
        int size = total_rows/process_count;
        segments.at(i).start = size*i;
        segments.at(i).end = size*i + size;

        //get segments for shared memory location segment for each process
        segments.at(i).shm_start = i*per_procc_mem;
        segments.at(i).shm_end = i*per_procc_mem + per_procc_mem;

        //if at the last process, check to see if the division is not even
        if(i==process_count-1)
            segments.at(i).end += total_rows%process_count;
    }

    //create the final results vector
    //reserve enough space to be able to merge all of our processes' stuff
    std::vector<ResultType> final_results;
    std::vector<float> copy;
    final_results.reserve(process_count*num_max);

    //create start and end chrono time points
    std::chrono::time_point<std::chrono::system_clock> start, end;
    std::map<int, double> times;

    //loop through the 4 different sizes of vectors
    for(i=0; i<sizes_count; i++) {


        std::cout << "\n\n==============TEST BEGIN==================\n" << std::endl;
        //run the test:
        copy = generateScottVector(sizes[i]);
        final_results = circularSubvectorMatch(sizes[i], &copy, &points, num_max, 0, total_rows, 0, 0, NULL, true);
        copy.clear();
        //run the test
        if(runTest(sizes[i], &final_results, num_max)) {
            std::cout << "Test was SUCCESSFUL against vector: \n" << std::endl;
            std::cout << scottgs::vectorToCSV(generateScottVector(sizes[i])) << "\n\n" << std::endl;
        } else {
            std::cout << "Test FAILED against vector: \n" << std::endl;
            std::cout << scottgs::vectorToCSV(generateScottVector(sizes[i])) << "\n\n" << std::endl;
            //exit(-1);
        }
        std::cout << "Expected vector results" << std::endl;
        printVector(num_max, final_results, sizes[i]);
        std::cout << "\n\nGenerated vector results" << std::endl;
        printVector(num_max, generateVectorTest(sizes[i]), sizes[i]);

        final_results.clear();
        std::cout << "\n\n==============TEST END==================\n\n\n" << std::endl;


        //generate 30 random vectors of size size[i]
        for(int ii=0; ii< 30; ii++) {
            generated_vectors.at(ii) = generateRandomVector(sizes[i]);
        }

        //for testing purposes I am only doing 1.
        //generated_vectors.at(0).reserve(sizes[i]);
        //generated_vectors.at(0) = generateScottVector(sizes[i]);
        //loop through the (30 vectors specified in the description)
        for(j=0; j<30; j++) {
            //let the first process print the results.
            std::cout << "\n-----------------" << std::endl;
            std::cout << "Search: "<< sizes[i] << "-D" << std::endl;
            std::cout << "-----------------" << std::endl;
            //get an object of the process spawner class..
            start = std::chrono::system_clock::now();
            scottgs::Splitter splitter;
            for (int p = 0; p < process_count; ++p)
            {
                pid_t pid = splitter.spawn();
                if (pid < 0)
                {
                    std::cerr << "Could not fork!!! ("<< pid <<")" << std::endl;
                    // do not exit, we may have a process
                    // spawned from an earlier iteration
                    break;
                }
                if (0 == pid) // Child
                {
                    /* Attach shared memory */
                    if((shm = (float *)shmat(shmId, NULL, 0)) == (float *) -1)
                    {
                        std::cerr << "Init: Failed to attach shared memory (" << shmId << ")" << std::endl;
                        exit(1);
                    }

                    //let only process 0 to print this vector.
                    if(p == 0) {
                        std::cout << "\nSearch Vector: " << std::endl;
                        //print the created vector.
                        std::cout << scottgs::vectorToCSV(generated_vectors[j]) << std::endl;
                    }

                    //perform the test(delete this as it is not needed)
                    //pas the size of the search vector, the auto generated vector, the vectors from the file,
                    //the number of top results to return, and the offset from which to search.
                    circularSubvectorMatch(sizes[i], &generated_vectors[j], &points, num_max, segments.at(p).start, segments.at(p).end, segments.at(p).shm_start, segments.at(p).shm_end, shm, false);

                    //child exists
                    _exit(0);
                }//end if pid==0
            }
            //wait for all children before looping again..
            splitter.reap_all();
            //calculate end time.
            end = std::chrono::system_clock::now();
            //now perform printing and stuff. from shared memory.
            //print end time
            std::chrono::duration<double, std::milli> elapsed_seconds = end-start;
            //std::cout << "\nTime: " << elapsed_seconds.count() << " milliseconds\n" << std::endl;
            //keep a record of the time
            times[sizes[i]] += elapsed_seconds.count();

            //print top num_max results
            printResults(shm, num_max, process_count, sizes[i]);
        }//end 30 vectors loop
    }//end vector_size loop
    std::cout << "\n-----------------" << std::endl;
    std::cout << "Final Results:" << std::endl;
    std::cout << "-----------------\n" << std::endl;
    std::cout << "Size" << "|" << "Average Time" << std::endl;
    std::cout << "-----------------" << std::endl;
    std::cout << times[sizes[0]]/1 << " " << times[sizes[1]]/1 << " " << times[sizes[2]]/1 << " " << times[sizes[3]]/1 << std::endl;

    //detach the memory
    shmdt(shm);
    //delete shared memory after we are done with it.
    shmctl(shmId, IPC_RMID, NULL);

    return 0;
}
Esempio n. 5
0
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>


#define BIG_V_SIZE  3
#define LITTLE_V_SIZE 72
#define RV_COUNT 5

// -----------------------------------------------------------------------------
// Instantiate text vector, Big V vector, and array of vector sizes.
// -----------------------------------------------------------------------------
std::vector<float> SV = generateRandomVector(BIG_V_SIZE);
std::vector<float> TV(LITTLE_V_SIZE);
std::vector<std::vector<float>> V(LITTLE_V_SIZE * BIG_V_SIZE);
std::vector<std::vector<float>> RV(LITTLE_V_SIZE * RV_COUNT);
std::string br = "\n\n###";
static const int vectorSize[] = {9,11,17,29};

// -----------------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------------

int main( int argc, char **argv ){
   
   Stopwatch timer;