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 <cstdio>
- #include <ctime>
- cudaError_t addWithCuda(long long potential_prime, bool *not_prime, int threadsize, int blockssize, int border_number);
- __global__ void is_prime_Kernel(long long prime, bool *not_prime, int border_number)
- {
- int i = blockDim.x * blockIdx.x + threadIdx.x;
- if (not_prime[0])
- {
- return;
- }
- if (i < border_number)
- {
- if (prime % (i+2) == 0)
- {
- not_prime[0] = true;
- }
- }
- }
- int main()
- {
- long long potential_prime;
- int threadsSize;
- int blocksSize;
- int border_number;
- bool not_prime[1];
- not_prime[0] = true;
- printf("Podaj liczbe: \n");
- scanf("%llu", &potential_prime);
- printf("Twoja liczba to: %llu\n", potential_prime);
- border_number = sqrt(potential_prime);
- if (border_number < 1024)
- {
- blocksSize = 1;
- threadsSize = border_number;
- }
- else
- {
- blocksSize = border_number / 1024;
- threadsSize = border_number % 104;
- }
- printf("Watki: %i\n", threadsSize);
- printf("Bloki: %i\n", blocksSize);
- printf("Ilosc liczb do sprawdzenia: %i\n", border_number);
- cudaError_t cudaStatus = addWithCuda(potential_prime, not_prime, threadsSize, blocksSize, border_number);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "addWithCuda failed!");
- return 1;
- }
- return 0;
- }
- cudaError_t addWithCuda(long long potential_prime, bool *not_prime, int threadsize, int blockssize, int border_number)
- {
- int *devisors = 0;
- bool* prime;
- float computing_time = 0;
- cudaError_t cudaStatus;
- cudaEvent_t start_counting, stop_counting;
- cudaStatus = cudaEventCreate(&start_counting);
- cudaStatus = cudaEventCreate(&stop_counting);
- cudaEventRecord(start_counting, 0);
- cudaStatus = cudaSetDevice(0);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
- goto Error;
- }
- cudaStatus = cudaMalloc((void**)&prime, 1 * sizeof(bool));
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMalloc failed!");
- goto Error;
- }
- is_prime_Kernel <<< blockssize, threadsize >>>(potential_prime, prime, border_number);
- cudaStatus = cudaGetLastError();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
- goto Error;
- }
- cudaStatus = cudaDeviceSynchronize();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
- goto Error;
- }
- cudaStatus = cudaMemcpy(not_prime, prime, 1 * sizeof(bool), cudaMemcpyDeviceToHost);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- goto Error;
- }
- cudaEventRecord(stop_counting, 0);
- cudaEventSynchronize(stop_counting);
- cudaEventElapsedTime(&computing_time, start_counting, stop_counting);
- cudaEventDestroy(start_counting);
- cudaEventDestroy(stop_counting);
- if (!not_prime[0])
- {
- printf("Jest pierwsza!\n");
- }
- else
- {
- printf("Nie jest pierwsza!\n");
- }
- printf("Obliczono w czasie: %.4f sekund\n", computing_time/1000);
- Error:
- cudaFree(devisors);
- cudaFree(prime);
- return cudaStatus;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement