forked from BenLangmead/bowtie2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbowtie_main.cpp
More file actions
128 lines (119 loc) · 3.92 KB
/
Copy pathbowtie_main.cpp
File metadata and controls
128 lines (119 loc) · 3.92 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
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
/*
* Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "tokenize.h"
#include "ds.h"
#ifdef ENABLE_x86_64_v3
#include <unistd.h>
#endif
using namespace std;
extern "C" {
int bowtie(int argc, const char **argv);
}
#ifdef ENABLE_x86_64_v3
// taken from https://attractivechaos.wordpress.com/2017/09/04/on-cpu-dispatch/
#define SIMD_SSE 0x1
#define SIMD_SSE2 0x2
#define SIMD_SSE3 0x4
#define SIMD_SSE4_1 0x8
#define SIMD_SSE4_2 0x10
#define SIMD_AVX 0x20
#define SIMD_AVX2 0x40
#define SIMD_AVX512F 0x80
unsigned x86_simd(void) {
unsigned eax, ebx, ecx, edx, flag = 0;
#ifdef _MSC_VER
int cpuid[4];
__cpuid(cpuid, 1);
eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3];
#else
asm volatile("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (1));
#endif
if (edx>>25&1) flag |= SIMD_SSE;
if (edx>>26&1) flag |= SIMD_SSE2;
if (ecx>>0 &1) flag |= SIMD_SSE3;
if (ecx>>19&1) flag |= SIMD_SSE4_1;
if (ecx>>20&1) flag |= SIMD_SSE4_2;
if (ecx>>28&1) flag |= SIMD_AVX;
if (ebx>>5 &1) flag |= SIMD_AVX2;
if (ebx>>16&1) flag |= SIMD_AVX512F;
return flag;
}
void check_x86_64_v3(int argc, const char **argv) {
if ((x86_simd() & SIMD_AVX2) && (argc<126)) {
const char* new_argv[128]; // should always be enough, but above check enforces it, too
const char * org_path = argv[0];
// Append -v256 to the original path
const int fn_len = strlen(org_path);
char *new_path = (char*) malloc(fn_len+16);
memcpy(new_path,org_path,fn_len);
strncpy(new_path+fn_len,"-v256",15);
for (int i=1; i<=argc; i++) new_argv[i] = argv[i]; // all but first the same, also copy final NULL
new_argv[0] = new_path;
// now replace the executable with new variant
// assuming the executable exists... execvp will gracefully fail else, which is OK
execvp(new_argv[0], (char *const *)new_argv);
// we should never get out of the above call
fprintf(stderr,"[WARNING] Failed to launch x86-64-v3 version, staying with default\n");
}
}
#endif
/**
* Bowtie main function. It is placed in a separate source file to
* make it slightly easier to compile Bowtie as a library.
*
* If the user specifies -A <file> as the first two arguments, main
* will interpret that file as having one set of command-line arguments
* per line, and will dispatch each batch of arguments one at a time to
* bowtie.
*/
int main(int argc, const char **argv) {
#ifdef ENABLE_x86_64_v3
check_x86_64_v3(argc, argv);
#endif
if(argc > 2 && strcmp(argv[1], "-A") == 0) {
const char *file = argv[2];
ifstream in;
in.open(file);
char buf[4096];
int lastret = -1;
while(in.getline(buf, 4095)) {
EList<string> args;
args.push_back(string(argv[0]));
tokenize(buf, " \t", args);
const char **myargs = (const char**)malloc(sizeof(char*)*args.size());
for(size_t i = 0; i < args.size(); i++) {
myargs[i] = args[i].c_str();
}
if(args.size() == 1) continue;
lastret = bowtie((int)args.size(), myargs);
free(myargs);
}
if(lastret == -1) {
cerr << "Warning: No arg strings parsed from " << file << endl;
return 0;
}
return lastret;
} else {
return bowtie(argc, argv);
}
}