Hello everyone,
I’m quite new to C++ and have just started CUDA programming as well for my master’s thesis, but all i have encountered so far is thousands different problems.
For days now i have struggled trying to get a program to read a text file with a list of words (patterns), then send it to the GPU for further processing (pattern matching against some other text).
For this i have found a text file with “bad words”, and i want to try run a kernel for each line from this file.
(The goal of the program as a whole is to be like a tiny prototype of an IDS, scanning network packets like Snort for known signatures, however that seems a few years away as of now).
I have tried many sorts of guides but none of them seem to manage to send a dynamic character array to the GPU, (or a list of character arrays).
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Dict{
char *list;
int offsets[480]; //Can this be made dynamically? its 480 patterns in the badwords.txt
int max_len;
int num_patterns;
void genDictionary(const char * filename){
max_len =0;
num_patterns =0;
char * cstr;
string temp, line;
int i =0;
int curr_offset =0;
ifstream myfile (filename);
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile, line);
cstr = new char[line.size()+1];
strcpy(cstr, line.c_str());
temp += cstr;
curr_offset +=line.size();
offsets[i] = curr_offset;
num_patterns++;
if (max_len < line.size()) max_len = line.size();
i++;
}
myfile.close();
}
list = new char[temp.size()+1];
strcpy(list, temp.c_str());
}
};
__global__ void runKernel( char** g_list, int *g_offset)
{
printf("Hello thread %d\n", threadIdx.x);
//Do something with the input
}
void runEngine(){
Dict dictionary;
dictionary.genDictionary("badwords.txt");
int num_threads = dictionary.num_patterns;
int mem_size_list = sizeof(char) *strlen(dictionary.list);
int mem_size_offsets = sizeof(int)* sizeof(dictionary.offsets);
// allocate device memory for list
char** d_list;
cudaMalloc( (void**) &d_list, mem_size_list);
cudaMemcpy( d_list, dictionary.list, mem_size_list, cudaMemcpyHostToDevice) ;
//// allocate device memory for offset
int* d_offset;
cudaMalloc( (void**) &d_offset, mem_size_offsets);
cudaMemcpy( d_offset, dictionary.offsets, mem_size_offsets, cudaMemcpyHostToDevice) ;
// copy host memory to device
runKernel<<<1,1>>>(d_list, d_offset); //start 1 kernel just for testing
}
int main(){
runEngine();
cudaDeviceReset();
return 0;
}
Can anyone please see if they locate the error, or give link to some guides that manage list of character arrays.
Appreciate all efforts made!
I added my whole project file, + the badwords.txt as attachment as well. (VS 2010 project).
Windows 7 64bit (program 32bit)
CUDA 4.1
SDK 4.1
Compiled as 2.0 compute.
Test.rar (3.05 MB)