-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_working_string.cpp
More file actions
80 lines (63 loc) · 2.51 KB
/
main_working_string.cpp
File metadata and controls
80 lines (63 loc) · 2.51 KB
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
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#include <CL/cl2.hpp>
#include <iostream>
#include <vector>
#include <fstream>
#include <numeric>
const std::string kernel_code = R"CLC(
__kernel void gc_count(
__global const char* dna_sequence,
__global int* gc_counts,
const int sequence_length) {
int thread_id = get_global_id(0);
int local_gc_count = 0;
for (int i = thread_id; i < sequence_length; i += get_global_size(0)) {
char base = dna_sequence[i];
if (base == 'G' || base == 'C') {
local_gc_count++;
}
}
gc_counts[thread_id] = local_gc_count;
}
)CLC";
int main() {
// Input DNA sequence
std::string dna_sequence = "ATGCGGTTGCACGTAAGCGG"; // Example DNA sequence
int sequence_length = dna_sequence.length();
// Convert DNA sequence to char array
std::vector<char> dna_array(dna_sequence.begin(), dna_sequence.end());
// Load OpenCL platform and device
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl::Platform platform = platforms[0];
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
cl::Device device = devices[0];
cl::Context context(device);
cl::CommandQueue queue(context, device);
// Create buffers
cl::Buffer buffer_dna(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, dna_array.size(), dna_array.data());
cl::Buffer buffer_gc_counts(context, CL_MEM_WRITE_ONLY, sizeof(int) * sequence_length);
// Create and build the program
cl::Program program(context, kernel_code);
program.build({device});
// Create the kernel
cl::Kernel kernel(program, "gc_count");
kernel.setArg(0, buffer_dna);
kernel.setArg(1, buffer_gc_counts);
kernel.setArg(2, sequence_length);
// Execute the kernel
size_t global_work_size = 256; // Number of threads
queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(global_work_size));
// Retrieve results
std::vector<int> gc_counts(sequence_length);
queue.enqueueReadBuffer(buffer_gc_counts, CL_TRUE, 0, sizeof(int) * sequence_length, gc_counts.data());
// Compute total GC count
int total_gc_count = std::accumulate(gc_counts.begin(), gc_counts.end(), 0);
// Calculate GC content percentage
double gc_content_percentage = (static_cast<double>(total_gc_count) / sequence_length) * 100.0;
// Output the result
std::cout << "GC Content: " << gc_content_percentage << "%" << std::endl;
return 0;
}