19 size_t buffer_index = ~0;
24 std::unordered_map<std::string, StringLocation> m_string_map;
26 std::vector<char*> m_device_string_buffers;
27 std::vector<char*> m_host_string_buffers;
29 size_t m_current_buffer_offset;
36 : m_buffer_size(buffer_size)
37 , m_current_buffer_offset(0)
39 m_device_string_buffers.reserve(32);
40 m_host_string_buffers.reserve(32);
43 checkCudaErrors(cudaMalloc(&s, m_buffer_size *
sizeof(
char)));
44 m_device_string_buffers.emplace_back(s);
45 m_host_string_buffers.emplace_back(
new char[m_buffer_size]);
47 m_empty_string_pointer = get_string_pointer(
"");
51 for(
auto s : m_device_string_buffers)
53 for(
auto s : m_host_string_buffers)
65 if(s.empty() || s ==
"")
67 return m_empty_string_pointer;
69 return get_string_pointer(s);
75 auto str = std::string{s};
76 auto it = m_string_map.find(str);
77 char* device_string =
nullptr;
78 char* host_string =
nullptr;
79 unsigned int str_length = 0;
81 if(it != m_string_map.end())
83 auto& loc = it->second;
84 device_string = m_device_string_buffers[loc.buffer_index] + loc.offset;
85 host_string = m_host_string_buffers[loc.buffer_index] + loc.offset;
86 str_length =
static_cast<unsigned int>(loc.size - 1);
90 auto zero_end_length = str.size() + 1;
91 auto& loc = m_string_map[str];
93 if(m_current_buffer_offset + zero_end_length > m_buffer_size)
96 checkCudaErrors(cudaMalloc(&s, m_buffer_size *
sizeof(
char)));
97 m_device_string_buffers.emplace_back(s);
98 m_host_string_buffers.emplace_back(
new char[m_buffer_size]);
99 m_current_buffer_offset = 0;
102 auto device_buffer = m_device_string_buffers.back();
103 auto host_buffer = m_host_string_buffers.back();
106 host_buffer[m_current_buffer_offset + str.size()] =
'\0';
107 std::memcpy(host_buffer + m_current_buffer_offset, str.data(), str.size());
110 checkCudaErrors(cudaMemcpy(device_buffer + m_current_buffer_offset,
111 host_buffer + m_current_buffer_offset,
113 cudaMemcpyHostToDevice));
115 loc.buffer_index = m_host_string_buffers.size() - 1;
116 loc.offset = m_current_buffer_offset;
117 loc.size = zero_end_length;
119 m_current_buffer_offset += zero_end_length;
121 device_string = device_buffer + loc.offset;
122 host_string = host_buffer + loc.offset;
123 str_length =
static_cast<unsigned int>(loc.size - 1);
125 return StringPointer{device_string, host_string, str_length};