Sunday, February 28, 2016

Lab 6 - Vectorization

The assignment for this lab is to fill two arrays with 1000 integers and add them together in a third array. The goal is to have the compiler recognize that it can use vectorization.
The code I wrote for this is:
#include <stdio.h>
#include <time.h> 
int main(void) {
srand(time(NULL));
    int arraySize = 1000;
    int arrayOne[arraySize], arrayTwo[arraySize], arrayThree[arraySize];
    for (int i = 0; i < arraySize; i++) {
        arrayOne[i] = rand() % 1000;
        arrayTwo[i] = rand() % 1000;
    }
 
    for (int j = 0; j < arraySize; j++) {
        arrayThree[j] = arrayOne[j] + arrayTwo[j];
    }
 
    long sum = 0;
    for (int k = 0; k < arraySize; k++) {
        sum += arrayThree[k];
    }
    printf("%d\n", sum);
    return 0;
}
To compile the code on archie we use the following compiler command:
gcc -x c vec.C -o vec -O3 -std=c99 -g
 I have not yet tested the code on archie, and only on my own pc. So that's on my TO-DO list.

No comments:

Post a Comment