#include #include "stdlib.h" #include "rasclib.h" #include "string.h" #include "sys/time.h" #include "time.h" #define RAM_SIZE (131072) #define SIZE2 (128*131072/8) // SIZE2 is Number of Bytes of Data // RAM_SIZE is the Number of 128 Bit words // Input Structure Four 32 bit values typedef struct { unsigned int A; unsigned int B; unsigned int C; unsigned int D; } __attribute__((packed)) INvar; // Output Structure Two 64 bit values typedef struct { unsigned long long A; unsigned long long B; } __attribute__((packed)) OUTvar; // Creation of Input/Output Arrays // Array Size is same as RAM_SIZE in Mit C // Array Size is the number of 128 bit values INvar DATAin[RAM_SIZE]; OUTvar DATAout[RAM_SIZE]; OUTvar DATAout2[RAM_SIZE]; INvar DATAout3[RAM_SIZE]; void sw_compute(INvar *, OUTvar *); int main(int argc, char **argv) { int algorithm_id; int i; int res; char resrv_name[300]; char alg_name[300]; int num_devices; suseconds_t start, end; double dif; struct timeval tv; srand(1); strcpy(resrv_name, "my_fpga"); // alg_name is the name given to the bit stream in devmgr strcpy(alg_name, "multi4_large"); num_devices = 1; for (i = 0; i < RAM_SIZE; i++) { DATAin[i].A = rand() % 10000 + 1; DATAin[i].B = rand() % 10000 + 1; DATAin[i].C = rand() % 10000 + 1; DATAin[i].D = rand() % 10000 + 1; } sw_compute(DATAin, DATAout); gettimeofday(&tv, NULL); start = tv.tv_usec; res = rasclib_resource_reserve(num_devices, resrv_name); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"reserve failed at %d: %d\n", __LINE__, res); rasclib_perror("reserve", res); return 1; } res = rasclib_resource_configure(alg_name, num_devices, resrv_name); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"configure failed at %d: %d\n", __LINE__, res); rasclib_perror("configure", res); return 1; } algorithm_id = rasclib_algorithm_open(alg_name, RASCLIB_BUFFERED_IO); if (algorithm_id == RASCLIB_FAIL) { fprintf(stderr,"open failed at %d: %d\n", __LINE__, algorithm_id); rasclib_perror("open", res); return 1; } // Sends a Pointer location to the data needed for each of the two // FPGA Memory inputs. To figure out the variable name on the FPGA // look in the bitstream.cfg file. The Size of the memory must be // included as well. SIZE2 is the number of Bytes of Mem used, // 128 bits * 2048 / 8 bits per byte res = rasclib_algorithm_send(algorithm_id, "memA0_in", &DATAin, SIZE2); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"send failed at %d: %d\n", __LINE__, res); return 1; } res = rasclib_algorithm_send(algorithm_id, "memB0_in", &DATAout2, SIZE2); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"send failed at %d: %d\n", __LINE__, res); return 1; } // Tells the FPGA to Run rasclib_algorithm_go(algorithm_id); // Sends a Pointer location to the data locations for each of the two // FPGA Memory outputs. To figure out the variable name on the FPGA // look in the bitstream.cfg file. The Size of the memory must be // included as well. SIZE2 is the number of Bytes of Mem used, // 128 bits * 2048 / 8 bits per byte res = rasclib_algorithm_receive(algorithm_id, "memB0_out", &DATAout2, SIZE2); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"recv failed at %d: %d\n", __LINE__, res); return 1; } res = rasclib_algorithm_receive(algorithm_id, "memA0_out", &DATAout3, SIZE2); if (res != RASCLIB_SUCCESS) { fprintf(stderr,"recv failed at %d: %d\n", __LINE__, res); return 1; } // Waits for all the commands in the commit are done rasclib_algorithm_commit(algorithm_id, NULL); rasclib_algorithm_wait(algorithm_id); // Closes the memory inputs/outputs // Returns/releases the reservation of the devices rasclib_algorithm_close(algorithm_id); rasclib_resource_return(alg_name, num_devices); rasclib_resource_release(num_devices, resrv_name); gettimeofday(&tv, NULL); end = tv.tv_usec; dif = difftime (end,start); printf("It took the FPGA %.1lf microseconds\n", dif); // The following is used to compare the data out of the FPGA to the // calculations done on the CPU. This is done using the memcmp // function. if (memcmp(DATAout, DATAout2, SIZE2) == 0 ) { printf("success\n"); return 0; } printf("Failed\n"); return 2; } void sw_compute(INvar *Ivar, OUTvar *Ovar) { int i; suseconds_t start, end; double dif; struct timeval tv; gettimeofday(&tv, NULL); start = tv.tv_usec; //time (&start); for (i=0;i