Skip to content
Snippets Groups Projects
Commit 74b0c13f authored by Prakalp Srivastava's avatar Prakalp Srivastava
Browse files

Added testcase for autogeneration of visc IR

parent 54fabb0d
No related branches found
No related tags found
No related merge requests found
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define WA 1024
#define HA 1024
#define WB 1024
#define HB WA
#define WC WB
#define HC HA
// Thread block size
#define BLOCK_SIZE 16
// Allocates a matrix with random float entries.
void randomInit(float* data, int size) {
for (int i = 0; i < size; ++i)
data[i] = rand() / (float)RAND_MAX;
}
//////////////////////////////////////////////////////////////////////////////
//! Loads a Program file.
//!
//! @return the source string if succeeded, 0 otherwise
//! @param cFilename program filename
//! @param szFinalLength returned length of the code string
//////////////////////////////////////////////////////////////////////////////
// Check bool
int isEqual(float a, float b) {
return (fabs(a-b) < 0.001);
}
// Check Results
__attribute__ ((noinline)) int checkResults(float* A, float* B, float* C) {
unsigned int size_A = WA * HA;
unsigned int size_B = WB * HB;
unsigned int size_C = WC * HC;
unsigned int bytesC = sizeof(float) * size_C;
float* goldC = (float*) malloc(bytesC);
for (int i=0; i < HC; i++) {
for (int j=0; j < WC; j++) {
goldC[i*WC + j] = 0;
for (int k=0; k < HB; k++) {
goldC[i*WC + j] += A[i*WA + k] * B[k*WB + j];
}
if(!isEqual(goldC[i*WC + j], C[i*WC + j])) {
printf("Mismatch at %d,%d --- C = %f and goldC = %f\n", i, j, C[i*WC+j], goldC[i*WC+j]);
return 0;
}
}
}
return 1; // Success
}
// Dummy visc node execution call
//void __visc__node(void kernel (float*, float*, float*, unsigned, unsigned), int numDims, void* dims, int numInputs, void* inputs, int numOutputs, void* outputs);
void matrixMul(float* A, float* B, float* C, unsigned k, unsigned n) {
printf("Entered function\n");
int tx = get_global_id(0); //2D Global Thread ID x
int ty = get_global_id(1); //2D Global Thread ID y
//int tx = get_global_id(0); //2D Global Thread ID x
//int ty = get_global_id(1); //2D Global Thread ID y
printf("Computing element (%d, %d)\n", tx, ty);
// Initialize accumulator
float res = 0.0f;
// Perform dot-product of row-column
for (int i = 0; i < k; i++) {
printf("Accessing k = %d, A[%d], B[%d]\n", k, ty*k+i, i*n+tx);
res += A[ty*k+i] * B[i*n+tx];
}
printf("Result computed\n");
// Write in device memory
C[ty*n+tx] = res;
printf("Result written to C\n");
}
// Main
int main(int argc, char** argv) {
// seed for rand()
srand(2006);
// Allocate host memory for matrices A and B
unsigned int size_A = WA * HA;
unsigned int bytes_A = sizeof(float) * size_A;
float* h_A = (float*) malloc(bytes_A);
unsigned int size_B = WB * HB;
unsigned int bytes_B = sizeof(float) * size_B;
float* h_B = (float*) malloc(bytes_B);
// Initialize host memory
randomInit(h_A, size_A);
randomInit(h_B, size_B);
/*
// Print A and B
printf("\n\nMatrix A\n");
for(int i = 0; i < size_A; i++)
{
printf("%f ", h_A[i]);
if(((i + 1) % WA) == 0)
printf("\n");
}
printf("\n\nMatrix B\n");
for(int i = 0; i < size_B; i++)
{
printf("%f ", h_B[i]);
if(((i + 1) % WB) == 0)
printf("\n");
}
*/
// Allocate host memory for the result matrix C
unsigned int size_C = WC * HC;
unsigned int bytes_C = sizeof(float) * size_C;
float* h_C = (float*) malloc(bytes_C);
// Compute using OpenCL
//matrixMul(h_A, h_B, h_C, WA, WB);
//__visc__node(matrixMul, 2, WB, HA, 3, h_A, h_B, h_C, 0);
__visc__node(matrixMul, 2, WB, HA, 5, h_A, h_B, h_C, WA, WB, 0);
if(checkResults(h_A, h_B, h_C))
printf("\nPass!\n");
else
printf("\nFailed!\n");
printf("\nDone!\n");
// Deallocate memory
free(h_A);
free(h_B);
free(h_C);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment