Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdio.h>
- using namespace std;
- cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);
- void mojaFcja(bool *czyPierwsza, long int *sprawdzana, unsigned int size);
- __global__ void addKernel(int *c, const int *a, const int *b)
- {
- int i = threadIdx.x;
- c[i] = a[i] + b[i];
- }
- __global__ void checkKernel(long int* bliczba, bool *pierwsza)
- {
- if (*bliczba % 2 != 0)
- {
- int dzielnik = (threadIdx.x * 2) + 1;
- if (dzielnik == 1)
- pierwsza[threadIdx.x] = true;
- else if (*bliczba % dzielnik != 0)
- {
- pierwsza[threadIdx.x] = true;
- }
- }
- else
- {
- pierwsza[threadIdx.x] = false;
- }
- }
- int main()
- {
- const int arraySize = 5;
- const int a[arraySize] = { 1, 2, 3, 4, 5 };
- const int b[arraySize] = { 10, 20, 30, 40, 50 };
- int c[arraySize] = { 0 };
- long int sprawdzana;
- scanf("%i", &sprawdzana);
- int size = sprawdzana / 2 - 1;;
- //bool *czyPierwsza = new bool[size];
- bool czyPierwsza[1000];
- // Add vectors in parallel.
- mojaFcja(czyPierwsza, &sprawdzana, size);
- bool wynik = true;
- for (int i = 0; i < size; i++)
- {
- if (!czyPierwsza[i])
- wynik = false;
- }
- if (wynik)
- printf("pierwsza");
- else
- printf("nie");
- getchar();
- getchar();
- // cudaDeviceReset must be called before exiting in order for profiling and
- // tracing tools such as Nsight and Visual Profiler to show complete traces.
- return 0;
- }
- // Helper function for using CUDA to add vectors in parallel.
- void mojaFcja(bool *czyPierwsza,long int *sprawdzana, unsigned int size)
- {
- bool *dev_czyP;
- long int *dev_s;
- // Choose which GPU to run on, change this on a multi-GPU system.
- cudaSetDevice(0);
- // Allocate GPU buffers for three vectors (two input, one output) .
- cudaMalloc((void**)&dev_czyP, size * sizeof(bool));
- cudaMalloc((void**)&dev_s, sizeof(long int));
- // Copy input vectors from host memory to GPU buffers.
- cudaMemcpy(dev_czyP, czyPierwsza, size * sizeof(bool), cudaMemcpyHostToDevice);
- cudaMemcpy(dev_s, sprawdzana, sizeof(long int), cudaMemcpyHostToDevice);
- // Launch a kernel on the GPU with one thread for each element.
- checkKernel<<<1, size >>>(dev_s, dev_czyP);
- // Check for any errors launching the kernel
- cudaGetLastError();
- // cudaDeviceSynchronize waits for the kernel to finish, and returns
- // any errors encountered during the launch.
- cudaDeviceSynchronize();
- // Copy output vector from GPU buffer to host memory.
- cudaMemcpy(czyPierwsza, dev_czyP, size * sizeof(bool), cudaMemcpyDeviceToHost);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement