Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <math.h>
- #include <stdio.h>
- #include <iostream>
- #include <cstdlib>
- cudaError_t CheckPrimeCUDA(bool *upsik, unsigned long long *dzielnik, unsigned long long *pitca, unsigned long long size);
- __global__ void CheckPrimeKernel(bool *upsik, unsigned long long int *dzielnik, unsigned long long *pitca)
- {
- *dzielnik = 5 + (threadIdx.x + blockDim.x*blockIdx.x ) * 6;
- if ((*pitca % *dzielnik) == 0)
- *upsik = false;
- if ((*pitca % (*dzielnik + 2)) == 0)
- *upsik = false;
- }
- int main()
- {
- unsigned long long superliczba = 2305843009213693951;
- unsigned long long dzielnik;
- bool sprawdzto = 1;
- if (superliczba % 2 == 0)
- sprawdzto = 0;
- if (superliczba % 3 == 0)
- sprawdzto = 0;
- #pragma omp parallel for
- for (dzielnik = 5; dzielnik <= sqrt(superliczba); dzielnik += 6)
- {
- if (superliczba%dzielnik == 0)
- sprawdzto = 0;
- break;
- if (superliczba%dzielnik + 2 == 0)
- sprawdzto = 0;
- break;
- }
- if (sprawdzto == 1)
- printf("pizza supreme\n\n");
- else
- {
- printf("woah\n\n");
- }
- system("pause");
- // Add vectors in parallel.
- // cudaError_t cudaStatus = CheckPrimeKernel(sprawdzto, superliczba, dzielnik);
- //if (cudaStatus != cudaSuccess) {
- // fprintf(stderr, "addWithCuda failed!");
- // return 1;
- // }
- // printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
- // c[0], c[1], c[2], c[3], c[4]);
- // cudaDeviceReset must be called before exiting in order for profiling and
- // tracing tools such as Nsight and Visual Profiler to show complete traces.
- // cudaStatus = cudaDeviceReset();
- // if (cudaStatus != cudaSuccess) {
- // fprintf(stderr, "cudaDeviceReset failed!");
- // return 1;
- // }
- return 0;
- }
- // Helper function for using CUDA to add vectors in parallel.
- cudaError_t CheckPrimeCUDA(bool *upsik, unsigned long long *dzielnik, unsigned long long *pitca, unsigned long long size)
- {
- unsigned long long int *wsk_pitca = 0;
- unsigned long long int *wsk_dzielnik = 0;
- bool * wsk_upsik = 0;
- size = sizeof(unsigned long long int);
- cudaError_t cudaStatus;
- // Choose which GPU to run on, change this on a multi-GPU system.
- cudaStatus = cudaSetDevice(0);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
- goto Error;
- }
- // Allocate GPU buffers for three vectors (two input, one output) .
- cudaStatus = cudaMalloc((void**)&wsk_upsik, size * sizeof(int));
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMalloc failed!");
- goto Error;
- }
- cudaStatus = cudaMalloc((void**)&wsk_pitca, size * sizeof(int));
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMalloc failed!");
- goto Error;
- }
- cudaStatus = cudaMalloc((void**)&wsk_dzielnik, size * sizeof(int));
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMalloc failed!");
- goto Error;
- }
- // Copy input vectors from host memory to GPU buffers.
- cudaStatus = cudaMemcpy(wsk_pitca, pitca, size * sizeof(unsigned long long), cudaMemcpyHostToDevice);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- goto Error;
- }
- cudaStatus = cudaMemcpy(wsk_dzielnik, dzielnik, size * sizeof(unsigned long long), cudaMemcpyHostToDevice);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- goto Error;
- }
- // Launch a kernel on the GPU with one thread for each element.
- CheckPrimeKernel<<<2048, 2048>>>(wsk_upsik, wsk_pitca, wsk_dzielnik);
- // Check for any errors launching the kernel
- cudaStatus = cudaGetLastError();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
- goto Error;
- }
- // cudaDeviceSynchronize waits for the kernel to finish, and returns
- // any errors encountered during the launch.
- cudaStatus = cudaDeviceSynchronize();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
- goto Error;
- }
- // Copy output vector from GPU buffer to host memory.
- cudaStatus = cudaMemcpy(upsik, wsk_upsik, size * sizeof(unsigned long long), cudaMemcpyDeviceToHost);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- goto Error;
- }
- Error:
- cudaFree(wsk_pitca);
- cudaFree(wsk_dzielnik);
- cudaFree(wsk_upsik);
- return cudaStatus;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement