Skip to content
Snippets Groups Projects
Commit b647d875 authored by Matei BARBU's avatar Matei BARBU
Browse files

cuda: advanced: exercises: fixups

parent d25441d9
No related branches found
No related tags found
1 merge request!6Lab6 support
EXEC := $(notdir $(CURDIR))
LDFLAGS := -lstdc++
include ../../../Makefile
......@@ -34,8 +34,9 @@ __global__ void kernel_write(unsigned long long int *data,
// TODO
}
// do not modify validateDB and main
//
////////////////////////////////////////////////////////////////////////////////
// DO NOT modify below
void validateDB(unsigned long long int *database, int dbSize, int expNZElem) {
bool isValid = true;
int numNZElem = 0;
......@@ -109,4 +110,4 @@ int main(void) {
cudaFree(data);
cudaFree(database);
return 0;
}
\ No newline at end of file
}
EXEC := $(notdir $(CURDIR))
LDFLAGS := -lstdc++
include ../../../Makefile
# Sume particulare
Fiind dat un vector, `data`, cu valori mai mici decât 10, generați un nou
vector `result` după următoarea formulă.
$$result(i) = \sum_{j=0}^{data(i)} data(j)$$
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#define NUM_ELEM 128
......@@ -11,71 +9,72 @@ using namespace std;
// workers will compute sum on first N elements
__global__ void worker() {
// TODO, compute sum and store in result
}
// TODO
// master will launch threads to compute sum on first N elements
__global__ void master() {
// TODO, schedule worker threads
}
// TODO
// master will launch threads to compute sum on first N elements
__global__ void master() {
// TODO, schedule worker threads
}
void generateData(int *data, int num) {
srand(time(0));
void generateData(int *data, int num) {
srand(time(0));
for (int i = 0; i < num; i++) {
data[i] = rand() % 8 + 2;
}
for (int i = 0; i < num; i++) {
data[i] = rand() % 8 + 2;
}
}
void print(int *data, int num) {
for (int i = 0; i < num; i++) {
cout << data[i] << " ";
}
cout << endl;
void print(int *data, int num) {
for (int i = 0; i < num; i++) {
cout << data[i] << " ";
}
// check
// each element result[i] should be sum of first data[i] elements of data[i]
bool checkResult(int *data, int num, int *result) {
for (int i = 0; i < num; i++) {
int sum = 0;
for (int j = 0; j < data[i] && j < num; j++) {
sum += data[j];
}
if (result[i] != sum) {
cout << "Error at " << i << ", requested sum of first " << data[i]
<< " elem, got " << result[i] << endl;
return false;
}
cout << endl;
}
// check
// each element result[i] should be sum of first data[i] elements of data
bool checkResult(int *data, int num, int *result) {
for (int i = 0; i < num; i++) {
int sum = 0;
for (int j = 0; j < data[i] && j < num; j++) {
sum += data[j];
}
return true;
if (result[i] != sum) {
cout << "Error at " << i << ", requested sum of first " << data[i]
<< " elem, got " << result[i] << endl;
return false;
}
}
int main(int argc, char *argv[]) {
int *data = NULL;
cudaMallocManaged(&data, NUM_ELEM * sizeof(int));
int *result = NULL;
cudaMallocManaged(&result, NUM_ELEM * sizeof(int));
return true;
}
generateData(data, NUM_ELEM);
int main(int argc, char *argv[]) {
int *data = NULL;
cudaMallocManaged(&data, NUM_ELEM * sizeof(int));
// TODO schedule master threads and pass data/result/num
master<<<1, 1>>>();
cudaDeviceSynchronize();
int *result = NULL;
cudaMallocManaged(&result, NUM_ELEM * sizeof(int));
print(data, NUM_ELEM);
print(result, NUM_ELEM);
generateData(data, NUM_ELEM);
if (checkResult(data, NUM_ELEM, result)) {
cout << "Result OK" << endl;
} else {
cout << "Result ERR" << endl;
}
// TODO schedule master threads and pass data/result/num
master<<<1, 1>>>();
cudaDeviceSynchronize();
cudaFree(data);
cudaFree(result);
print(data, NUM_ELEM);
print(result, NUM_ELEM);
return 0;
if (checkResult(data, NUM_ELEM, result)) {
cout << "Result OK" << endl;
} else {
cout << "Result ERR" << endl;
}
cudaFree(data);
cudaFree(result);
return 0;
}
EXEC := $(notdir $(CURDIR))
LDFLAGS := -lstdc++
include ../../../../Makefile
EXEC := $(notdir $(CURDIR))
LDFLAGS := -lstdc++
include ../../../../Makefile
EXEC := $(notdir $(CURDIR))
LDFLAGS := -lstdc++
include ../../../../Makefile
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