My code is working with no error messages but it’s not functioning "it does not write the secret word on the output file. Can you tell me if there is any error in my code?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <cuda_runtime.h>
#include <cuda.h>
#include <device_launch_parameters.h>
#define INPUT_FILE_NAME "encodedfile.txt"
#define LINE_SIZE 100
#define LINES_COUNT 15360
#define SPECIAL_CHAR ','
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
// Function used to read the text file into a 1-d array and return a pointer to it
char *read_file(FILE *file);
// Serial program to find out the secret word
void find_secret_word_serial(char *file_as_line_h, char **secret_word_h);
void write_to_file(char *secret_word_h);
__global__ void find_secret_word_parallel(char *file_as_line, char *secret_word, int n){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx <= n && idx>0){ // if current index does not exceed n: the total file length
if (file_as_line[idx-1] == SPECIAL_CHAR){
secret_word[idx] = file_as_line[idx];
}
}
}
int main(int argc, char **argv){
struct timeval stop, start;
float elapsed;
char *file_as_line_h = NULL;
FILE *file = fopen(INPUT_FILE_NAME, "r");
char *secret_word_h;
size_t n = LINE_SIZE * LINES_COUNT;
secret_word_h = (char *) calloc(n, sizeof(char));
file_as_line_h= read_file(file);
// Calculating the elapsing start time
gettimeofday(&start, NULL);
// Check if 's' passed to main function then run serial program else, run parallel program
if (argc == 2 && !strcmp(argv[1], "s")) {
find_secret_word_serial(file_as_line_h, &secret_word_h);
}else if (argc == 2 && !strcmp(argv[1], "p")){
size_t size = n * sizeof(char);
int num_blocks;
int block_size;
char *file_as_line_d, *secret_word_d;
// Allocating memory space for variables on Device
gpuErrchk(cudaMalloc((void **) &file_as_line_d, size));
gpuErrchk(cudaMalloc((void **) &secret_word_d, size));
printf("%s\n", file_as_line_d);
// Copy our data from Host to Device
gpuErrchk(cudaMemcpy(file_as_line_d, file_as_line_h, n, cudaMemcpyHostToDevice));
// Do calculation on Device side
block_size = 1024;
num_blocks = (int) (n / block_size);
find_secret_word_parallel <<< num_blocks, block_size>>> (file_as_line_d, secret_word_d, n);
gpuErrchk(cudaPeekAtLastError());
// Copying data back from Device to Host
gpuErrchk(cudaMemcpy(secret_word_h, secret_word_d, n, cudaMemcpyDeviceToHost));
gpuErrchk(cudaDeviceSynchronize());
// Freeing the allocated memory space
cudaFree(file_as_line_d);
cudaFree(secret_word_d);
}else{
printf("Error while receiving the arguments ro main function!\n");
exit(-1);
}
// Calculating the elapsing end time
gettimeofday(&stop, NULL);
elapsed = (stop.tv_sec - start.tv_sec) * 1000.0f
+ (stop.tv_usec-start.tv_usec) / 1000.0f;
printf("%s\n", secret_word_h);
printf("\nCode executed in %f milliseconds or %f seconds.\n", elapsed, elapsed/1000);
write_to_file(secret_word_h);
// Closing and freeing the memory
fclose(file);
free(secret_word_h);
}
// Function used to read the text file into a 1-d array and return a pointer to it
char *read_file(FILE *file){
char *fileAsOneLine;
char line[LINE_SIZE];
int i;
if (!file){
printf("Could not open the text file! Check the name or existence of your file!\n");
exit(-1);
}
// Allocate memory for pointers to pointers to chars according to lines count
fileAsOneLine = (char *)calloc((size_t)LINES_COUNT * LINE_SIZE, sizeof(char));
if (fileAsOneLine == NULL) {
printf("Cannot allocate space for lines of the file!\n");
exit(-2);
}
for (i = 0; i < LINES_COUNT; i++) {
fgets(line, LINE_SIZE, file);
strcat(fileAsOneLine, line);
}
/* Return the array of chars */
return fileAsOneLine;
}
// Serial program to find out the secret word
void find_secret_word_serial(char *file_as_line_h, char **secret_word_h){
int i;
int secret_word_i = 0;
char *temp = *secret_word_h;
for (i = 0; i < strlen(file_as_line_h); i++) {
if (file_as_line_h[i]== SPECIAL_CHAR){
temp[secret_word_i++] = file_as_line_h[++i];
}
}
}
// Function to write out the secret word on "decoded.txt" file
void write_to_file(char *secret_word_h){
FILE *output = fopen("decoded.txt", "w");
fprintf(output, "%s", secret_word_h);
fclose(output);
}
encodedfile.txt (1.48 MB)