emergency sync 2

This commit is contained in:
Ziad Arafat committed 2023-12-05 21:00:20 -07:00
1 parent 81fc0cc85a
commit 290d50b1f8
10 files changed
+177 -93

No files matched your search

Binary file not shown.
+11 -1
View File
@@ -39,6 +39,9 @@ int* generate_array(int size) {
return array;
}
void printPerformanceMetrics(const char* metric, double value) {
printf("\"%s\": %.6f\n", metric, value); // Display 6 decimal places
}
int main(int argc, char const *argv[])
{
@@ -65,7 +68,14 @@ int main(int argc, char const *argv[])
end_time = omp_get_wtime();
printf("count: %d, ", count);
printf("Time taken: %f\n", end_time - start_time);
// print performance metrics
double elapsed_time = end_time - start_time;
// convert seconds to nanoseconds
elapsed_time *= 1000000000;
printPerformanceMetrics("elapsed_time", elapsed_time);
free(array);
return 0;
+42
View File
@@ -40,3 +40,45 @@ count: 994, Time taken: 0.000740
count: 994, Time taken: 0.000732
count: 994, Time taken: 0.000727
count: 994, Time taken: 0.000860
1 threads
count: 3311, "elapsed_time": 51627.052017
count: 3311, "elapsed_time": 38612.983190
count: 3311, "elapsed_time": 37320.016418
count: 3311, "elapsed_time": 35726.989154
count: 3311, "elapsed_time": 33542.979509
2 threads
count: 3311, "elapsed_time": 102031.975985
count: 3311, "elapsed_time": 69361.005444
count: 3311, "elapsed_time": 56096.003391
count: 3311, "elapsed_time": 52599.003538
count: 3311, "elapsed_time": 55513.984989
4 threads
count: 3311, "elapsed_time": 161484.000273
count: 3311, "elapsed_time": 145333.993714
count: 3311, "elapsed_time": 406976.032536
count: 3414, "elapsed_time": 134133.035317
count: 3414, "elapsed_time": 164358.993061
8 threads
count: 3414, "elapsed_time": 443905.009888
count: 3414, "elapsed_time": 210986.007005
count: 3414, "elapsed_time": 259828.986600
count: 3414, "elapsed_time": 231715.966947
count: 3414, "elapsed_time": 214643.951040
16 threads
count: 3414, "elapsed_time": 431702.006608
count: 3414, "elapsed_time": 423746.998422
count: 3414, "elapsed_time": 395253.009629
count: 3414, "elapsed_time": 669698.987622
count: 3414, "elapsed_time": 400123.011786
32 threads
count: 3414, "elapsed_time": 807899.981737
count: 3414, "elapsed_time": 789654.965047
count: 3414, "elapsed_time": 877259.997651
count: 3414, "elapsed_time": 804393.028375
count: 3414, "elapsed_time": 858054.030687
64 threads
count: 3414, "elapsed_time": 2202283.008955
count: 3414, "elapsed_time": 1627230.027225
count: 3414, "elapsed_time": 1570594.031364
count: 3414, "elapsed_time": 1586124.009918
count: 3414, "elapsed_time": 2054583.979771
BIN
View File
Binary file not shown.
+96 -83
View File
@@ -1,6 +1,8 @@
// example cuda program
//
// Created by Ziad Arafat
// cuda program to count the number of threes in an array.
// Uses all the threads in a block to count the number of threes in a block.
#define NUM_BLOCKS 1
#define NUM_THREADS 1024
#include <iostream>
#include <cuda.h>
@@ -9,111 +11,122 @@
#include <stdlib.h>
using namespace std;
// kernel function to add up the elements of a vector using reduce algorithm
__global__ void reduce(int *g_idata, int *g_odata)
void printPerformanceMetrics(const std::string& metric, double value) {
std::cout << "\"" << metric << "\": " << value << std::endl;
}
__global__ void count_threes(int *data, int *count, int N)
{
extern __shared__ int sdata[];
int i = blockIdx.x * blockDim.x + threadIdx.x;
int local_count = 0;
// each thread loads one element from global to shared mem
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
sdata[tid] = g_idata[i];
if (i < N) {
local_count = (data[i] == 3) ? 1 : 0;
}
extern __shared__ int sdata[];
sdata[threadIdx.x] = local_count;
__syncthreads();
// do reduction in shared mem
for (unsigned int s = 1; s < blockDim.x; s *= 2)
{
if (tid % (2 * s) == 0)
{
sdata[tid] += sdata[tid + s];
// Perform reduction in shared memory
for (unsigned int s = 1; s < blockDim.x; s *= 2) {
int index = 2 * s * threadIdx.x;
if (index < blockDim.x) {
sdata[index] += sdata[index + s];
}
__syncthreads();
}
// write result for this block to global mem
if (tid == 0)
g_odata[blockIdx.x] = sdata[0];
}
__global__ void vector_add(int *a, int *b, int *c, int N)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < N)
{
c[i] = a[i] + b[i];
// Write result for this block to global memory
if (threadIdx.x == 0) {
count[blockIdx.x] = sdata[0];
}
}
int main(int argc, char const *argv[])
{
// size of the vector
int N = pow(2, 11);
// size of the vector in bytes
size_t bytes = N * sizeof(int);
// host vectors
int *h_a, *h_b;
// allocate memory for host vectors
h_a = (int *)malloc(bytes);
h_b = (int *)malloc(bytes);
// initialize host vectors
for (int i = 0; i < N; i++)
// Parse command line arguments
if (argc != 2)
{
// generate a random number
srand(1); // set the seed to current time
h_a[i] = rand() % 1000;
// cout << h_a[i] << " ";
cout << "Usage: " << argv[0] << " N" << endl;
return 1;
}
int N = atoi(argv[1]);
size_t free_available_gpu_memory, total_available_gpu_memory;
cudaError_t err = cudaMemGetInfo(
&free_available_gpu_memory,
&total_available_gpu_memory
);
if (err != cudaSuccess ) {
cout << cudaGetErrorString(err)
<< " in " << __FILE__ << " at line "
<< __LINE__ << endl;
}
// device vectors
// create cuda events for timing
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Allocate host memory
int *h_a, *h_b, *h_c;
h_a = new int[N];
h_b = new int[N];
h_c = new int[N];
// Initialize host arrays
for (int i = 0; i < N; i++)
{
h_a[i] = rand() % 4;
}
// Allocate device memory
int *d_a, *d_b;
cudaMalloc((void **)&d_a, N * sizeof(int));
cudaMalloc((void **)&d_b, N * sizeof(int));
// allocate memory for device vectors
cudaMalloc(&d_a, bytes);
cudaMalloc(&d_b, bytes);
// Copy data from host to device memory
cudaMemcpy(d_a, h_a, N * sizeof(int), cudaMemcpyHostToDevice);
// copy data from host to device
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);
// number of threads per block
int NUM_THREADS = 1024;
// record start time
cudaEventRecord(start);
// number of blocks
int NUM_BLOCKS = (N + NUM_THREADS - 1) / NUM_THREADS;
// Launch kernel
int total_count = 0;
// number of bytes for shared memory
int SHMEM_BYTES = NUM_THREADS * sizeof(int);
// execute kernel
reduce<<<NUM_BLOCKS, NUM_THREADS, SHMEM_BYTES>>>(d_a, d_b);
// copy data from device to host
cudaMemcpy(h_b, d_b, bytes, cudaMemcpyDeviceToHost);
// print the result
cout << "The sum is " << h_b[0] << endl;
// test if result is correct
int sum = 0;
for (int i = 0; i < N; i++)
{
sum += h_a[i];
for (int i = 0; i < NUM_BLOCKS; i++) {
count_threes<<<NUM_BLOCKS, NUM_THREADS, NUM_THREADS * sizeof(int)>>>(d_a, d_b, N);
cudaMemcpy(h_b, d_b, N * sizeof(int), cudaMemcpyDeviceToHost);
total_count += h_b[i];
}
cout << "The sum is " << sum << endl;
// free device memory
// record end time
cudaEventRecord(stop);
// wait for the stop event to complete
cudaEventSynchronize(stop);
// calculate elapsed time
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, start, stop);
// destroy cuda events
cudaEventDestroy(start);
cudaEventDestroy(stop);
// free memory
cudaFree(d_a);
cudaFree(d_b);
delete[] h_a;
delete[] h_b;
delete[] h_c;
// convert milliseconds to nano seconds
milliseconds *= 1000000;
// free host memory
free(h_a);
free(h_b);
return 0;
cout << "Total number of threes in the array: " << total_count << endl;
printPerformanceMetrics("executionTime", milliseconds);
}
+11 -3
View File
@@ -61,10 +61,8 @@ public class CountThrees implements Runnable {
// compute the elapsed time in nanoseconds
long duration = (endTime - startTime);
// convert to seconds
double seconds = (double)duration / 1_000_000_000.0;
System.out.println("Time taken: " + seconds + " seconds");
printPerformanceMetrics("executionTime", duration);
}
private int startIndex;
@@ -76,6 +74,16 @@ public class CountThrees implements Runnable {
elements = elem;
}
public static void printPerformanceMetrics(
String metric,
double value
)
{
System.out.printf("\"%s\": %f%n", metric, value);
}
// Overload of run method in the Thread class
public void run() {
// Count the number of threes
Binary file not shown.
+12 -5
View File
@@ -35,6 +35,10 @@ int *generate_array(int size, int world_rank) {
}
}
void printPerformanceMetrics(const char* metric, double value) {
printf("\"%s\": %f\n", metric, value);
}
int main(int argc, char **argv) {
@@ -48,6 +52,7 @@ int main(int argc, char **argv) {
// record start time
double start_time, end_time;
double elapsed_time;
if (world_rank == 0) {
start_time = MPI_Wtime();
}
@@ -110,11 +115,13 @@ int main(int argc, char **argv) {
// record end time
if (world_rank == 0) {
end_time = MPI_Wtime();
printf(
"Total computation time: %f seconds\n",
end_time - start_time
);
end_time = MPI_Wtime();
elapsed_time = end_time - start_time;
// convert seconds to nanoseconds
elapsed_time *= 1000000000;
printPerformanceMetrics("executionTime", elapsed_time);
}
// Finalize the MPI environment
MPI_Finalize();
Binary file not shown.
+5 -1
View File
@@ -55,6 +55,10 @@ int* generate_array(int size) {
return array;
}
void printPerformanceMetrics(const char* metric, double value) {
printf("\"%s\": %f\n", metric, value);
}
/**
* @brief Function to count the number of threes in an array
* @param array The array to be counted
@@ -165,7 +169,7 @@ int main(int argc, char **argv) {
free(array);
printf("parallel time: %ld\n", parallel_time);
printPerformanceMetrics("executionTime", parallel_time);
return 0;
}