Project Files/Testing Source/main.cpp
void normalisingVectors() {
    //Define basic Vec3 struct
    struct Vec3 { float x, y, z; };

    //Store the number of worker threads to create
    unsigned int threadCount;

    //Loop until valid input
    do {
        //Clear the screen
        system("CLS");

        //Display prompt to the user
        getInput(threadCount, "Enter the number of Worker threads to create (32 maximum): ");
    } while (threadCount > 32);

    //Add some space on screen
    printf("\n\n\n");

    //Create the Task Manager
    if (AsynchTasks::TaskManager::create(threadCount)) {
        //Create the basic input manager
        BasicInput input = BasicInput(VK_ESCAPE, VK_SPACE);

        //Display initial instructions
        printf("Hold 'SPACE' to add a new Task to normalise 3,000,000 Vector 3 objects (Multiple Task Test)\n\n");

        //Loop until the input breaks the loop
        while (!input.keyPressed(VK_ESCAPE)) {
            //Update the input
            input.update();

            //Update the Task Manager
            AsynchTasks::TaskManager::update();

            //Output a single '.'
            printf(".");

            //Determine if space was pressed
            if (input.keyDown(VK_SPACE)) {
                //Create a new Task
                AsynchTasks::Task<std::pair<unsigned int, float>> newTask = AsynchTasks::TaskManager::createTask<std::pair<unsigned int, float>>();

                //Force callback to print on main
                newTask->callbackOnUpdate = true;

                //Set the functions
                newTask->process = [&]() -> std::pair<unsigned int, float> {
                    //Define the number of vectors to create
                    const unsigned int ARRAY_SIZE = 3000000;

                    //Create an array of 1 mill vectors
                    Vec3* vectors = new Vec3[ARRAY_SIZE];

                    //Store the number of vectors that were successfully normalised
                    unsigned int totalNormal = 0;
                    float averageNonNormal = 0.f;
                    
                    //Give the vectors random values
                    for (unsigned int i = 0; i < ARRAY_SIZE; i++) {
                        //Assign random axis values
                        vectors[i].x = randomRange(-500.f, 500.f);
                        vectors[i].y = randomRange(-500.f, 500.f);
                        vectors[i].z = randomRange(-500.f, 500.f);

                        //Get the magnitude of the vector
                        float mag = sqrtf(SQU(vectors[i].x) + SQU(vectors[i].y) + SQU(vectors[i].z));

                        //Check the magnitude is valid
                        if (mag) {
                            //Divide along the axis
                            vectors[i].x /= mag;
                            vectors[i].y /= mag;
                            vectors[i].z /= mag;
                        }

                        //Check if the magnitude has been normalised
                        mag = sqrtf(SQU(vectors[i].x) + SQU(vectors[i].y) + SQU(vectors[i].z));
                        if (mag == 0.f || mag == 1.f)
                            totalNormal++;
                        else averageNonNormal += mag;
                    }

                    //Delete the vector objects
                    delete[] vectors;

                    //Return the final count
                    return std::pair<unsigned int, float>(totalNormal, averageNonNormal /= (float)(ARRAY_SIZE - totalNormal));
                };
                newTask->callback = [&](std::pair<unsigned int, float>& pVal) {
                    printf("\nNormalized %u of 3,000,000 Vector 3 objects. The average non-normalised magnitude was %f\n", pVal.first, pVal.second);
                };

                //Add the task to the Task Manager
                if (AsynchTasks::TaskManager::addTask(newTask))
                    printf("\n\nAdded new Task to the Manager. Processing...\n");
                else printf("\n\nFailed to add the new task the Manager.\n");
            }

            //Slow main down to viewable pace
            Sleep(100);
        }
    }

    //Display error message
    else printf("Failed to create the Asynchronous Task Manager\n");

    //Destroy the the Task Manager
    AsynchTasks::TaskManager::destroy();
}
/*
    normalisingVectors - Normalise a large number of randomly sized vectors to test speed
    Author: Mitchell Croft
    Created: 24/08/2016
    Modified: 12/01/2017
*/
Normalise a large number of randomly sized vectors to test speed
Author: Mitchell Croft
Created: 24/08/2016
Modified: 12/01/2017
Normalise a large number of randomly sized vectors to test speed Author : Mitchell Croft Created : 24 / 08 / 2016 Modified : 12 / 01 / 2017
void normalisingVectors() {
    struct Vec3 { float x, y, z; };
    unsigned int threadCount;
    do {
        system("CLS");
        getInput(threadCount, "Enter the number of Worker threads to create (32 maximum): ");
    } while (threadCount > 32);
    printf("\n\n\n");
    if (AsynchTasks::TaskManager::create(threadCount)) {
        BasicInput input = BasicInput(VK_ESCAPE, VK_SPACE);
        printf("Hold 'SPACE' to add a new Task to normalise 3,000,000 Vector 3 objects (Multiple Task Test)\n\n");
        while (!input.keyPressed(VK_ESCAPE)) {
            input.update();
            AsynchTasks::TaskManager::update();
            printf(".");
            if (input.keyDown(VK_SPACE)) {
                AsynchTasks::Task<std::pair<unsigned int, float>> newTask = AsynchTasks::TaskManager::createTask<std::pair<unsigned int, float>>();
                newTask->callbackOnUpdate = true;
                newTask->process = [&]() -> std::pair<unsigned int, float> {
                    const unsigned int ARRAY_SIZE = 3000000;
                    Vec3* vectors = new Vec3[ARRAY_SIZE];
                    unsigned int totalNormal = 0;
                    float averageNonNormal = 0.f;
                    for (unsigned int i = 0; i < ARRAY_SIZE; i++) {
                        vectors[i].x = randomRange(-500.f, 500.f);
                        vectors[i].y = randomRange(-500.f, 500.f);
                        vectors[i].z = randomRange(-500.f, 500.f);
                        float mag = sqrtf(SQU(vectors[i].x) + SQU(vectors[i].y) + SQU(vectors[i].z));
                        if (mag) {
                            vectors[i].x /= mag;
                            vectors[i].y /= mag;
                            vectors[i].z /= mag;
                        }
                        mag = sqrtf(SQU(vectors[i].x) + SQU(vectors[i].y) + SQU(vectors[i].z));
                        if (mag == 0.f || mag == 1.f)
                            totalNormal++;
                        else averageNonNormal += mag;
                    }
                    delete[] vectors;
                    return std::pair<unsigned int, float>(totalNormal, averageNonNormal /= (float)(ARRAY_SIZE - totalNormal));
                };
                newTask->callback = [&](std::pair<unsigned int, float>& pVal) {
                    printf("\nNormalized %u of 3,000,000 Vector 3 objects. The average non-normalised magnitude was %f\n", pVal.first, pVal.second);
                };
                if (AsynchTasks::TaskManager::addTask(newTask))
                    printf("\n\nAdded new Task to the Manager. Processing...\n");
                else printf("\n\nFailed to add the new task the Manager.\n");
            }
            Sleep(100);
        }
    }
    else printf("Failed to create the Asynchronous Task Manager\n");
    AsynchTasks::TaskManager::destroy();
}
void normalisingVectors ( ) { struct Vec3 { float x , y , z ; } ; unsigned int threadCount ; do { system ( " " ) ; getInput ( threadCount , " " ) ; } while ( threadCount > 32 ) ; printf ( " \n \n \n " ) ; if ( AsynchTasks :: TaskManager :: create ( threadCount ) ) { BasicInput input = BasicInput ( VK_ESCAPE , VK_SPACE ) ; printf ( " \n \n " ) ; while ( ! input . keyPressed ( VK_ESCAPE ) ) { input . update ( ) ; AsynchTasks :: TaskManager :: update ( ) ; printf ( " " ) ; if ( input . keyDown ( VK_SPACE ) ) { AsynchTasks :: Task < std :: pair < unsigned int , float > > newTask = AsynchTasks :: TaskManager :: createTask < std :: pair < unsigned int , float > > ( ) ; newTask -> callbackOnUpdate = true ; newTask -> process = [ & ] ( ) -> std :: pair < unsigned int , float > { const unsigned int ARRAY_SIZE = 3000000 ; Vec3 * vectors = new Vec3 [ ARRAY_SIZE ] ; unsigned int totalNormal = 0 ; float averageNonNormal = 0.f ; for ( unsigned int i = 0 ; i < ARRAY_SIZE ; i ++ ) { vectors [ i ] . x = randomRange ( -500.f , 500.f ) ; vectors [ i ] . y = randomRange ( -500.f , 500.f ) ; vectors [ i ] . z = randomRange ( -500.f , 500.f ) ; float mag = sqrtf ( SQU ( vectors [ i ] . x ) + SQU ( vectors [ i ] . y ) + SQU ( vectors [ i ] . z ) ) ; if ( mag ) { vectors [ i ] . x /= mag ; vectors [ i ] . y /= mag ; vectors [ i ] . z /= mag ; } mag = sqrtf ( SQU ( vectors [ i ] . x ) + SQU ( vectors [ i ] . y ) + SQU ( vectors [ i ] . z ) ) ; if ( mag == 0.f || mag == 1.f ) totalNormal ++ ; else averageNonNormal += mag ; } delete [ ] vectors ; return std :: pair < unsigned int , float > ( totalNormal , averageNonNormal /= ( float ) ( ARRAY_SIZE - totalNormal ) ) ; } ; newTask -> callback = [ & ] ( std :: pair < unsigned int , float > & pVal ) { printf ( " \n \n " , pVal . first , pVal . second ) ; } ; if ( AsynchTasks :: TaskManager :: addTask ( newTask ) ) printf ( " \n \n \n " ) ; else printf ( " \n \n \n " ) ; } Sleep ( 100 ) ; } } else printf ( " \n " ) ; AsynchTasks :: TaskManager :: destroy ( ) ; }
normalisingVectors - Normalise a large number of randomly sized vectors to test speed
Author: Mitchell Croft
Created: 24/08/2016
Modified: 12/01/2017
normalisingVectors - Normalise a large number of randomly sized vectors to test speed Author : Mitchell Croft Created : 24 / 08 / 2016 Modified : 12 / 01 / 2017
//Define basic Vec3 struct //Store the number of worker threads to create //Loop until valid input //Clear the screen //Display prompt to the user //Add some space on screen //Create the Task Manager //Create the basic input manager //Display initial instructions //Loop until the input breaks the loop //Update the input //Update the Task Manager //Output a single '.' //Determine if space was pressed //Create a new Task //Force callback to print on main //Set the functions //Define the number of vectors to create //Create an array of 1 mill vectors //Store the number of vectors that were successfully normalised //Give the vectors random values //Assign random axis values //Get the magnitude of the vector //Check the magnitude is valid //Divide along the axis //Check if the magnitude has been normalised //Delete the vector objects //Return the final count //Add the task to the Task Manager //Slow main down to viewable pace //Display error message //Destroy the the Task Manager