-
Notifications
You must be signed in to change notification settings - Fork 26
/
gpusim.cpp
461 lines (398 loc) · 15.3 KB
/
gpusim.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* -------------------------------------------------------------------------
* Implements gpusim::GPUSimServer
* Reads a binary .fsim file and creates a FingerprintDB on GPU. Uses a local
* socket to communicate with querying processes. Fingerprint agnostic.
*
*
* Copyright Schrodinger LLC, All Rights Reserved.
--------------------------------------------------------------------------- */
#include "gpusim.h"
#include <QByteArray>
#include <QCoreApplication>
#include <QDataStream>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QLocalServer>
#include <QLocalSocket>
#include <QSize>
#include <QThread>
#include <QThreadPool>
#include <QTime>
#include <algorithm>
#include <exception>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include "fingerprintdb_cuda.h"
#include "local_qinfo.h"
using gpusim::Fingerprint;
using gpusim::FingerprintDB;
using std::map;
using std::pair;
using std::set;
using std::shared_ptr;
using std::string;
using std::vector;
const int DATABASE_VERSION = 3;
namespace gpusim
{
class DecompressAssignFPRunnable : public QRunnable
{
public:
QByteArray compressed_data;
vector<char>& fp_vector;
DecompressAssignFPRunnable(vector<char>& fp_in) : fp_vector(fp_in){};
void run() override
{
QByteArray fp_qba = qUncompress(compressed_data);
compressed_data.clear();
fp_vector.reserve(fp_qba.size());
fp_vector.insert(fp_vector.begin(), fp_qba.data(),
fp_qba.data() + fp_qba.size());
}
};
class DecompressAssignStringRunnable : public QRunnable
{
public:
QByteArray compressed_data;
vector<char*>& string_vector;
DecompressAssignStringRunnable(vector<char*>& string_in)
: string_vector(string_in){};
void run() override
{
QByteArray string_qba = qUncompress(compressed_data);
compressed_data.clear();
QDataStream string_stream(string_qba);
while (!string_stream.atEnd()) {
char* smi;
string_stream >> smi;
string_vector.push_back(smi);
}
string_qba.clear();
}
};
GPUSimServer::GPUSimServer(const QStringList& database_fnames, int gpu_bitcount)
{
qDebug() << "--------------------------";
qDebug() << "Starting up GPUSim Server";
qDebug() << "--------------------------";
qDebug() << "Utilizing" << get_gpu_count() << "GPUs for calculation.";
if (!setupSocket())
return;
for (auto database_fname : database_fnames) {
// Read from .fsim file into byte arrays
int fp_bitcount, fp_count;
QString dbkey;
vector<vector<char>> fingerprint_data;
vector<char*> smiles_vector;
vector<char*> ids_vector;
qDebug() << "Extracting data:" << database_fname;
extractData(database_fname, fp_bitcount, fp_count, dbkey,
fingerprint_data, smiles_vector, ids_vector);
qDebug() << "Finished extracting data";
// Create new FingerprintDB for querying on GPU
auto fps = std::shared_ptr<FingerprintDB>(
new FingerprintDB(fp_bitcount, fp_count, dbkey, fingerprint_data,
smiles_vector, ids_vector));
QFileInfo file_info(database_fname);
QString db_name = file_info.baseName();
m_databases[db_name] = fps;
}
// Now that we know how much total memory is required, divvy it up
// and allow the fingerprint databases to copy up data
size_t total_db_memory = 0;
unsigned int max_compounds_in_db = 0;
int max_fp_bitcount = 0;
for (auto db : m_databases) {
total_db_memory += db->getFingerprintDataSize();
max_compounds_in_db = std::max(max_compounds_in_db, db->count());
max_fp_bitcount =
std::max(max_fp_bitcount, db->getFingerprintBitcount());
}
unsigned int fold_factor = 1;
auto gpu_memory = get_available_gpu_memory();
// Reserve space for the indices vector during search
gpu_memory -= sizeof(int) * max_compounds_in_db;
qDebug() << "Database: " << total_db_memory / 1024 / 1024
<< "MB GPU Memory: " << gpu_memory / 1024 / 1024 << "MB";
if (total_db_memory > gpu_memory) {
fold_factor = ceilf((float) total_db_memory / (float) gpu_memory);
}
if (gpu_bitcount > 0) {
unsigned int arg_fold_factor = max_fp_bitcount / gpu_bitcount;
if (arg_fold_factor < fold_factor) {
throw std::invalid_argument(
"GPU bitset not sufficiently small to fit on GPU");
}
fold_factor = arg_fold_factor;
}
qInfo() << "Putting graphics card data up.";
if (fold_factor > 1) {
qDebug() << "Folding databases by at least" << fold_factor
<< "to fit in gpu memory";
}
if (usingGPU()) {
for (auto db : m_databases) {
db->copyToGPU(fold_factor);
}
}
qInfo() << "Finished putting graphics card data up.";
qInfo() << "Ready for searches.";
};
bool GPUSimServer::usingGPU()
{
return m_use_gpu && (get_gpu_count() != 0);
}
void GPUSimServer::extractData(const QString& database_fname, int& fp_bitcount,
int& fp_count, QString& dbkey,
vector<vector<char>>& fingerprint_data,
vector<char*>& smiles_vector,
vector<char*>& ids_vector)
{
QFile file(database_fname);
file.open(QIODevice::ReadOnly);
QDataStream datastream(&file);
// Set version so that files will be usable cross-release
datastream.setVersion(QDataStream::Qt_5_2);
int version;
datastream >> version;
if (version != DATABASE_VERSION) {
throw std::runtime_error(
"Database version incompatible with this GPUSim version");
}
char* dbkey_char;
datastream >> dbkey_char;
dbkey = dbkey_char;
delete[] dbkey_char;
datastream >> fp_bitcount;
datastream >> fp_count;
int fp_qba_count;
datastream >> fp_qba_count;
fingerprint_data.resize(fp_qba_count);
int current_qba = 1;
QThreadPool thread_pool;
for (auto& fp_vector : fingerprint_data) {
qDebug() << " loading FP " << current_qba++ << "of" << fp_qba_count;
auto thread = new DecompressAssignFPRunnable(fp_vector);
datastream >> thread->compressed_data;
thread_pool.start(thread);
}
int smi_qba_count;
datastream >> smi_qba_count;
vector<vector<char*>> smiles_data;
smiles_data.resize(smi_qba_count);
current_qba = 1;
for (auto& local_vector : smiles_data) {
qDebug() << " loading SMI " << current_qba++ << "of" << smi_qba_count;
auto thread = new DecompressAssignStringRunnable(local_vector);
datastream >> thread->compressed_data;
thread_pool.start(thread);
}
int id_qba_count;
datastream >> id_qba_count;
vector<vector<char*>> ids_data;
ids_data.resize(id_qba_count);
current_qba = 1;
for (auto& local_vector : ids_data) {
qDebug() << " loading ID " << current_qba++ << "of" << id_qba_count;
auto thread = new DecompressAssignStringRunnable(local_vector);
datastream >> thread->compressed_data;
thread_pool.start(thread);
}
qDebug() << " waiting for data processing threads to finish...";
thread_pool.waitForDone();
qDebug() << " merging smiles vectors";
for (auto& local_vector : smiles_data) {
smiles_vector.insert(smiles_vector.end(), local_vector.begin(),
local_vector.end());
local_vector.clear();
}
qDebug() << " merging ID vectors";
for (auto& local_vector : ids_data) {
ids_vector.insert(ids_vector.end(), local_vector.begin(),
local_vector.end());
local_vector.clear();
}
qDebug() << " finished merging vectors";
}
bool GPUSimServer::setupSocket()
{
const QString socket_name("gpusimilarity");
auto server = new QLocalServer(this);
if (!server->listen(socket_name)) {
QString socket_location = QString("/tmp/%1").arg(socket_name);
QFile::remove(socket_location);
if (!server->listen(socket_name)) {
qDebug() << "Server start failed on" << socket_location;
auto app = QCoreApplication::instance();
app->exit(1);
return false;
}
}
QLocalServer::connect(server, &QLocalServer::newConnection, this,
&GPUSimServer::newConnection);
return true;
}
void GPUSimServer::similaritySearch(
const Fingerprint& reference, const QString& dbname, const QString& dbkey,
unsigned int max_return_count, float similarity_cutoff, CalcType calc_type,
vector<char*>& results_smiles, vector<char*>& results_ids,
vector<float>& results_scores, unsigned long& approximate_result_count)
{
if (calc_type == CalcType::GPU) {
m_databases[dbname]->search(reference, dbkey, max_return_count,
similarity_cutoff, results_smiles,
results_ids, results_scores,
approximate_result_count);
} else {
m_databases[dbname]->search_cpu(reference, dbkey, max_return_count,
similarity_cutoff, results_smiles,
results_ids, results_scores,
approximate_result_count);
}
};
void GPUSimServer::newConnection()
{
auto server = dynamic_cast<QLocalServer*>(sender());
QLocalSocket* clientConnection = server->nextPendingConnection();
QObject::connect(clientConnection, &QLocalSocket::disconnected,
clientConnection, &QLocalSocket::deleteLater);
QObject::connect(clientConnection, &QLocalSocket::readyRead, this,
&GPUSimServer::incomingSearchRequest);
}
void GPUSimServer::searchDatabases(
const Fingerprint& query, int results_requested, float similarity_cutoff,
map<QString, QString>& dbname_to_key, vector<char*>& results_smiles,
vector<char*>& results_ids, vector<float>& results_scores,
unsigned long& approximate_result_count)
{
typedef pair<char*, char*> ResultData;
typedef pair<float, ResultData> SortableResult;
vector<SortableResult> sortable_results;
for (auto name_key_pair : dbname_to_key) {
const auto& local_dbname = name_key_pair.first;
const auto& local_key = name_key_pair.second;
vector<char*> l_results_smiles, l_results_ids;
vector<float> l_results_scores;
if (!m_databases.contains(local_dbname)) {
qDebug() << "Unknown database " << local_dbname << " requested.";
continue;
}
unsigned long local_approximate_result_count;
similaritySearch(query, local_dbname, local_key, results_requested,
similarity_cutoff,
usingGPU() ? CalcType::GPU : CalcType::CPU,
l_results_smiles, l_results_ids, l_results_scores,
local_approximate_result_count);
approximate_result_count += local_approximate_result_count;
for (unsigned int i = 0; i < l_results_smiles.size(); i++) {
sortable_results.push_back(SortableResult(
l_results_scores[i],
ResultData(l_results_smiles[i], l_results_ids[i])));
}
}
std::sort(sortable_results.begin(), sortable_results.end());
std::reverse(sortable_results.begin(), sortable_results.end());
map<string, string> smiles_to_ids;
for (auto result : sortable_results) {
const auto& smiles = result.second.first;
const auto& id = result.second.second;
if (smiles_to_ids.count(smiles) > 0) {
std::stringstream ss;
ss << smiles_to_ids[smiles];
ss << ";:;";
ss << id;
smiles_to_ids[smiles] = ss.str();
} else {
smiles_to_ids[smiles] = id;
}
if (smiles_to_ids.size() >= (unsigned int) results_requested)
break;
}
int written_count = 0;
set<string> smiles_written;
for (auto result : sortable_results) {
const auto& score = result.first;
const auto& smiles = result.second.first;
if (smiles_written.count(smiles) > 0)
continue;
smiles_written.insert(smiles);
results_scores.push_back(score);
results_smiles.push_back(smiles);
// strdup to hand memory handling off to receiver
results_ids.push_back(strdup(smiles_to_ids[smiles].c_str()));
if (++written_count >= results_requested)
break;
}
}
void GPUSimServer::incomingSearchRequest()
{
auto clientConnection = static_cast<QLocalSocket*>(sender());
// Read incoming Fingerprint binary and put it in Fingerprint object
QByteArray data = clientConnection->readAll();
QDataStream qds(&data, QIODevice::ReadOnly);
int database_search_count;
qds >> database_search_count;
map<QString, QString> dbname_to_key;
for (int i = 0; i < database_search_count; i++) {
char* dbname;
char* dbkey;
qds >> dbname;
qds >> dbkey;
dbname_to_key[QString(dbname)] = QString(dbkey);
delete[] dbname;
delete[] dbkey;
}
int results_requested;
// Used to guarantee Python reading the pipe reads correct request
int request_num;
qds >> request_num;
qds >> results_requested;
float similarity_cutoff;
qds >> similarity_cutoff;
QByteArray fp_data;
qds >> fp_data;
const int* raw_fp_data = reinterpret_cast<const int*>(fp_data.constData());
const int fp_int_size = fp_data.size() / sizeof(int);
Fingerprint query(fp_int_size);
query.assign(raw_fp_data, raw_fp_data + fp_int_size);
// Perform similarity search and return results in relevant vectors
vector<char*> results_smiles, results_ids;
vector<float> results_scores;
QElapsedTimer timer;
timer.start();
unsigned long approximate_result_count = 0;
searchDatabases(query, results_requested, similarity_cutoff, dbname_to_key,
results_smiles, results_ids, results_scores,
approximate_result_count);
qDebug() << "Search completed, time elapsed:"
<< (float) timer.elapsed() / 1000.0f;
// Create QByteArrays and QDataStreams to write to corresponding arrays
QByteArray output_smiles, output_ids, output_scores;
QDataStream smiles_stream(&output_smiles, QIODevice::WriteOnly);
QDataStream ids_stream(&output_ids, QIODevice::WriteOnly);
QDataStream scores_stream(&output_scores, QIODevice::WriteOnly);
for (unsigned int i = 0; i < results_smiles.size(); i++) {
smiles_stream << results_smiles[i];
ids_stream << results_ids[i];
scores_stream << results_scores[i];
}
// Transmit binary data to client and flush the buffered data
QByteArray ints_qba;
QDataStream ints_qds(&ints_qba, QIODevice::WriteOnly);
ints_qds << request_num;
ints_qds << (int) results_smiles.size();
ints_qds << (quint64) approximate_result_count;
clientConnection->write(ints_qba);
clientConnection->write(output_smiles);
clientConnection->write(output_ids);
clientConnection->write(output_scores);
clientConnection->flush();
}
Fingerprint GPUSimServer::getFingerprint(const int index, const QString& dbname)
{
return m_databases[dbname]->getFingerprint(index);
}
} // namespace gpusim