Hello. I must write console calculater. Which way is there other than this? I’d like to try calling the function not in the kernel, but in the block. How can I do that? And is it possible?
include <stdio.h>
include <math.h>
include
include “book.h”
global void add(int a, int b, int *c)
{
*c = a + b;
}
global void sub(int a, int b, int *c)
{
*c = a - b;
}
global void mul(int a, int b, int *c)
{
*c = a * b;
}
global void div(int a, int b, int *c)
{
*c = a / b;
}
int main(void) {
int c, a, b;
int op;
int *dev_c;
printf(“Vvedute a: “);
scanf(”%d”, &a);
printf("Vvedute b: ");
scanf("%d", &b);
printf("Vvedute operaciy: ");
scanf("%d", &op);
HANDLE_ERROR(cudaMalloc((void**)&dev_c, sizeof(int)));
if (op == 4)
{
add << <1, 1 >> >(a, b, dev_c);
}
else
if (op == 3)
{
sub << <1, 1 >> >(a, b, dev_c);
}
else
if (op == 2)
{
mul << <1, 1 >> >(a, b, dev_c);
}
else
if (op == 1)
{
div << <1, 1 >> >(a, b, dev_c);
}
HANDLE_ERROR(cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost));
printf("%d op %d =%d\n", a, b, c);
HANDLE_ERROR(cudaFree(dev_c));
system("pause");
return 0;
}