-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_parallel.cu
136 lines (101 loc) · 3.42 KB
/
kernel_parallel.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string.h>
#include <ctime>
#include <random>
#include <cuda.h>
#include <curand.h>
#include <curand_kernel.h>
#define K_mer 17
#define seed_num 10
#define loops 500
#include "functions_serial.cu"
#include "functions_parallel.cu"
using namespace std;
void print_char_data(char *data, int row, int col);
void print_float_data(float *data, int row, int col);
void pipelines(char *data_host, int data_num_cols, int data_num_rows, int pipeline_num, int motif_length, char *output_device);
int main() {
char *input; //Each DNA sequence is stored at a specific index in the array.
int DNA_len = 0;
// char mask[4] = { 'A', 'C', 'G', 'T' };
char *motifs_return;
cudaError_t cudaStatus;
char *input_device;
char *motifs_return_device;
int rowNum;
int row;
//DNA input file reading
ifstream file("datasets/cleaned/real/yst09r.txt");
string inputLineStr;
// get how many lines will be taken in the algorithm
rowNum = 0;
while (getline(file, inputLineStr)) {
if (inputLineStr.at(0) != '>') {
rowNum++;
DNA_len = inputLineStr.length();
}
}
file.close();
printf("Number of total rows: %d \n", rowNum);
printf("Number of total columns: %d \n", DNA_len);
// Allocate memory space for these lines with the fixed length: DNA_len
input = (char *)malloc(sizeof(char)*DNA_len*rowNum);
motifs_return = (char *)malloc(sizeof(char) * K_mer * rowNum * seed_num);
// Read lines and store them in the allocated memory space
ifstream infile("datasets/cleaned/real/yst09r.txt");
std::cout << "\nLoading data...\n";
row = 0;
int offset = 0;
while (getline(infile, inputLineStr)) { //Extracting file line by line.
if (inputLineStr.at(0) != '>') { //Only considers DNA strings, NOT sequence numbers.
if (row != 0) {
offset += DNA_len;
}
memcpy(input + offset, inputLineStr.c_str(), sizeof(char)*DNA_len);
row++;
}
}
infile.close();
printf("Loaded.\n");
clock_t begin = clock();
cudaStatus = cudaMalloc((void **)&input_device, sizeof(char) * DNA_len * rowNum);
if (cudaStatus != cudaSuccess) {
printf("cudaMalloc for input_device failed!\n");
}
cudaStatus = cudaMalloc((void **)&motifs_return_device, sizeof(char) * K_mer * rowNum * seed_num);
if (cudaStatus != cudaSuccess) {
printf("cudaMalloc for output_device failed!\n");
}
cudaStatus = cudaMemcpy(input_device, input, sizeof(char) * DNA_len * rowNum, cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
printf("cudaMemcpy for input_device H2D failed!\n");
}
pipelines(input_device, DNA_len, rowNum, seed_num, K_mer, motifs_return_device);
cudaStatus = cudaMemcpy(motifs_return, motifs_return_device, K_mer * rowNum * seed_num * sizeof(char), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
printf("cudaMemcpy for output_host D2H failed!\n");
}
clock_t end = clock();
double elapsed_secs_parallel = double(end - begin) / CLOCKS_PER_SEC;
printf("**Parallel: Total time taken = %.9f\n", elapsed_secs_parallel);
ofstream outf;
outf.open("datasets/sampled_parallel.txt");
for (int i = 0; i < K_mer * rowNum * seed_num; i++) {
if (i % K_mer == 0 && i != 0) {
outf << endl;
}
outf << motifs_return[i];
}
outf.close();
cudaFree(input_device);
cudaFree(motifs_return_device);
free(input);
return 0;
}