ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
helper_string.h
Go to the documentation of this file.
1/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of NVIDIA CORPORATION nor the names of its
12 * contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// These are helper functions for the SDK samples (string parsing, timers, etc)
29#ifndef COMMON_HELPER_STRING_H_
30#define COMMON_HELPER_STRING_H_
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <fstream>
35#include <string>
36
37#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
38#ifndef _CRT_SECURE_NO_DEPRECATE
39#define _CRT_SECURE_NO_DEPRECATE
40#endif
41#ifndef STRCASECMP
42#define STRCASECMP _stricmp
43#endif
44#ifndef STRNCASECMP
45#define STRNCASECMP _strnicmp
46#endif
47#ifndef STRCPY
48#define STRCPY(sFilePath, nLength, sPath) strcpy_s(sFilePath, nLength, sPath)
49#endif
50
51#ifndef FOPEN
52#define FOPEN(fHandle, filename, mode) fopen_s(&fHandle, filename, mode)
53#endif
54#ifndef FOPEN_FAIL
55#define FOPEN_FAIL(result) (result != 0)
56#endif
57#ifndef SSCANF
58#define SSCANF sscanf_s
59#endif
60#ifndef SPRINTF
61#define SPRINTF sprintf_s
62#endif
63#else // Linux Includes
64#include <string.h>
65#include <strings.h>
66
67#ifndef STRCASECMP
68#define STRCASECMP strcasecmp
69#endif
70#ifndef STRNCASECMP
71#define STRNCASECMP strncasecmp
72#endif
73#ifndef STRCPY
74#define STRCPY(sFilePath, nLength, sPath) strcpy(sFilePath, sPath)
75#endif
76
77#ifndef FOPEN
78#define FOPEN(fHandle, filename, mode) (fHandle = fopen(filename, mode))
79#endif
80#ifndef FOPEN_FAIL
81#define FOPEN_FAIL(result) (result == NULL)
82#endif
83#ifndef SSCANF
84#define SSCANF sscanf
85#endif
86#ifndef SPRINTF
87#define SPRINTF sprintf
88#endif
89#endif
90
91#ifndef EXIT_WAIVED
92#define EXIT_WAIVED 2
93#endif
94
95// CUDA Utility Helper Functions
96inline int stringRemoveDelimiter(char delimiter, const char *string) {
97 int string_start = 0;
98
99 while (string[string_start] == delimiter) {
100 string_start++;
101 }
102
103 if (string_start >= static_cast<int>(strlen(string) - 1)) {
104 return 0;
105 }
106
107 return string_start;
108}
109
110inline int getFileExtension(char *filename, char **extension) {
111 int string_length = static_cast<int>(strlen(filename));
112
113 while (filename[string_length--] != '.') {
114 if (string_length == 0) break;
115 }
116
117 if (string_length > 0) string_length += 2;
118
119 if (string_length == 0)
120 *extension = NULL;
121 else
122 *extension = &filename[string_length];
123
124 return string_length;
125}
126
127inline bool checkCmdLineFlag(const int argc, const char **argv,
128 const char *string_ref) {
129 bool bFound = false;
130
131 if (argc >= 1) {
132 for (int i = 1; i < argc; i++) {
133 int string_start = stringRemoveDelimiter('-', argv[i]);
134 const char *string_argv = &argv[i][string_start];
135
136 const char *equal_pos = strchr(string_argv, '=');
137 int argv_length = static_cast<int>(
138 equal_pos == 0 ? strlen(string_argv) : equal_pos - string_argv);
139
140 int length = static_cast<int>(strlen(string_ref));
141
142 if (length == argv_length &&
143 !STRNCASECMP(string_argv, string_ref, length)) {
144 bFound = true;
145 continue;
146 }
147 }
148 }
149
150 return bFound;
151}
152
153// This function wraps the CUDA Driver API into a template function
154template <class T>
155inline bool getCmdLineArgumentValue(const int argc, const char **argv,
156 const char *string_ref, T *value) {
157 bool bFound = false;
158
159 if (argc >= 1) {
160 for (int i = 1; i < argc; i++) {
161 int string_start = stringRemoveDelimiter('-', argv[i]);
162 const char *string_argv = &argv[i][string_start];
163 int length = static_cast<int>(strlen(string_ref));
164
165 if (!STRNCASECMP(string_argv, string_ref, length)) {
166 if (length + 1 <= static_cast<int>(strlen(string_argv))) {
167 int auto_inc = (string_argv[length] == '=') ? 1 : 0;
168 *value = (T)atoi(&string_argv[length + auto_inc]);
169 }
170
171 bFound = true;
172 i = argc;
173 }
174 }
175 }
176
177 return bFound;
178}
179
180inline int getCmdLineArgumentInt(const int argc, const char **argv,
181 const char *string_ref) {
182 bool bFound = false;
183 int value = -1;
184
185 if (argc >= 1) {
186 for (int i = 1; i < argc; i++) {
187 int string_start = stringRemoveDelimiter('-', argv[i]);
188 const char *string_argv = &argv[i][string_start];
189 int length = static_cast<int>(strlen(string_ref));
190
191 if (!STRNCASECMP(string_argv, string_ref, length)) {
192 if (length + 1 <= static_cast<int>(strlen(string_argv))) {
193 int auto_inc = (string_argv[length] == '=') ? 1 : 0;
194 value = atoi(&string_argv[length + auto_inc]);
195 } else {
196 value = 0;
197 }
198
199 bFound = true;
200 continue;
201 }
202 }
203 }
204
205 if (bFound) {
206 return value;
207 } else {
208 return 0;
209 }
210}
211
212inline float getCmdLineArgumentFloat(const int argc, const char **argv,
213 const char *string_ref) {
214 bool bFound = false;
215 float value = -1;
216
217 if (argc >= 1) {
218 for (int i = 1; i < argc; i++) {
219 int string_start = stringRemoveDelimiter('-', argv[i]);
220 const char *string_argv = &argv[i][string_start];
221 int length = static_cast<int>(strlen(string_ref));
222
223 if (!STRNCASECMP(string_argv, string_ref, length)) {
224 if (length + 1 <= static_cast<int>(strlen(string_argv))) {
225 int auto_inc = (string_argv[length] == '=') ? 1 : 0;
226 value = static_cast<float>(atof(&string_argv[length + auto_inc]));
227 } else {
228 value = 0.f;
229 }
230
231 bFound = true;
232 continue;
233 }
234 }
235 }
236
237 if (bFound) {
238 return value;
239 } else {
240 return 0;
241 }
242}
243
244inline bool getCmdLineArgumentString(const int argc, const char **argv,
245 const char *string_ref,
246 char **string_retval) {
247 bool bFound = false;
248
249 if (argc >= 1) {
250 for (int i = 1; i < argc; i++) {
251 int string_start = stringRemoveDelimiter('-', argv[i]);
252 char *string_argv = const_cast<char *>(&argv[i][string_start]);
253 int length = static_cast<int>(strlen(string_ref));
254
255 if (!STRNCASECMP(string_argv, string_ref, length)) {
256 *string_retval = &string_argv[length + 1];
257 bFound = true;
258 continue;
259 }
260 }
261 }
262
263 if (!bFound) {
264 *string_retval = NULL;
265 }
266
267 return bFound;
268}
269
278inline char *sdkFindFilePath(const char *filename,
279 const char *executable_path) {
280 // <executable_name> defines a variable that is replaced with the name of the
281 // executable
282
283 // Typical relative search paths to locate needed companion files (e.g. sample
284 // input data, or JIT source files) The origin for the relative search may be
285 // the .exe file, a .bat file launching an .exe, a browser .exe launching the
286 // .exe or .bat, etc
287 const char *searchPath[] = {
288 "./", // same dir
289 "./data/", // same dir
290
291 "../../../../Samples/<executable_name>/", // up 4 in tree
292 "../../../Samples/<executable_name>/", // up 3 in tree
293 "../../Samples/<executable_name>/", // up 2 in tree
294
295 "../../../../Samples/<executable_name>/data/", // up 4 in tree
296 "../../../Samples/<executable_name>/data/", // up 3 in tree
297 "../../Samples/<executable_name>/data/", // up 2 in tree
298
299 "../../../../Samples/0_Introduction/<executable_name>/", // up 4 in tree
300 "../../../Samples/0_Introduction/<executable_name>/", // up 3 in tree
301 "../../Samples/0_Introduction/<executable_name>/", // up 2 in tree
302
303 "../../../../Samples/1_Utilities/<executable_name>/", // up 4 in tree
304 "../../../Samples/1_Utilities/<executable_name>/", // up 3 in tree
305 "../../Samples/1_Utilities/<executable_name>/", // up 2 in tree
306
307 "../../../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 4 in tree
308 "../../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 3 in tree
309 "../../Samples/2_Concepts_and_Techniques/<executable_name>/", // up 2 in tree
310
311 "../../../../Samples/3_CUDA_Features/<executable_name>/", // up 4 in tree
312 "../../../Samples/3_CUDA_Features/<executable_name>/", // up 3 in tree
313 "../../Samples/3_CUDA_Features/<executable_name>/", // up 2 in tree
314
315 "../../../../Samples/4_CUDA_Libraries/<executable_name>/", // up 4 in tree
316 "../../../Samples/4_CUDA_Libraries/<executable_name>/", // up 3 in tree
317 "../../Samples/4_CUDA_Libraries/<executable_name>/", // up 2 in tree
318
319 "../../../../Samples/5_Domain_Specific/<executable_name>/", // up 4 in tree
320 "../../../Samples/5_Domain_Specific/<executable_name>/", // up 3 in tree
321 "../../Samples/5_Domain_Specific/<executable_name>/", // up 2 in tree
322
323 "../../../../Samples/6_Performance/<executable_name>/", // up 4 in tree
324 "../../../Samples/6_Performance/<executable_name>/", // up 3 in tree
325 "../../Samples/6_Performance/<executable_name>/", // up 2 in tree
326
327 "../../../../Samples/0_Introduction/<executable_name>/data/", // up 4 in tree
328 "../../../Samples/0_Introduction/<executable_name>/data/", // up 3 in tree
329 "../../Samples/0_Introduction/<executable_name>/data/", // up 2 in tree
330
331 "../../../../Samples/1_Utilities/<executable_name>/data/", // up 4 in tree
332 "../../../Samples/1_Utilities/<executable_name>/data/", // up 3 in tree
333 "../../Samples/1_Utilities/<executable_name>/data/", // up 2 in tree
334
335 "../../../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 4 in tree
336 "../../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 3 in tree
337 "../../Samples/2_Concepts_and_Techniques/<executable_name>/data/", // up 2 in tree
338
339 "../../../../Samples/3_CUDA_Features/<executable_name>/data/", // up 4 in tree
340 "../../../Samples/3_CUDA_Features/<executable_name>/data/", // up 3 in tree
341 "../../Samples/3_CUDA_Features/<executable_name>/data/", // up 2 in tree
342
343 "../../../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 4 in tree
344 "../../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 3 in tree
345 "../../Samples/4_CUDA_Libraries/<executable_name>/data/", // up 2 in tree
346
347 "../../../../Samples/5_Domain_Specific/<executable_name>/data/", // up 4 in tree
348 "../../../Samples/5_Domain_Specific/<executable_name>/data/", // up 3 in tree
349 "../../Samples/5_Domain_Specific/<executable_name>/data/", // up 2 in tree
350
351 "../../../../Samples/6_Performance/<executable_name>/data/", // up 4 in tree
352 "../../../Samples/6_Performance/<executable_name>/data/", // up 3 in tree
353 "../../Samples/6_Performance/<executable_name>/data/", // up 2 in tree
354
355 "../../../../Common/data/", // up 4 in tree
356 "../../../Common/data/", // up 3 in tree
357 "../../Common/data/" // up 2 in tree
358 };
359
360 // Extract the executable name
361 std::string executable_name;
362
363 if (executable_path != 0) {
364 executable_name = std::string(executable_path);
365
366#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
367 // Windows path delimiter
368 size_t delimiter_pos = executable_name.find_last_of('\\');
369 executable_name.erase(0, delimiter_pos + 1);
370
371 if (executable_name.rfind(".exe") != std::string::npos) {
372 // we strip .exe, only if the .exe is found
373 executable_name.resize(executable_name.size() - 4);
374 }
375
376#else
377 // Linux & OSX path delimiter
378 size_t delimiter_pos = executable_name.find_last_of('/');
379 executable_name.erase(0, delimiter_pos + 1);
380#endif
381 }
382
383 // Loop over all search paths and return the first hit
384 for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i) {
385 std::string path(searchPath[i]);
386 size_t executable_name_pos = path.find("<executable_name>");
387
388 // If there is executable_name variable in the searchPath
389 // replace it with the value
390 if (executable_name_pos != std::string::npos) {
391 if (executable_path != 0) {
392 path.replace(executable_name_pos, strlen("<executable_name>"),
393 executable_name);
394 } else {
395 // Skip this path entry if no executable argument is given
396 continue;
397 }
398 }
399
400#ifdef _DEBUG
401 printf("sdkFindFilePath <%s> in %s\n", filename, path.c_str());
402#endif
403
404 // Test if the file exists
405 path.append(filename);
406 FILE *fp;
407 FOPEN(fp, path.c_str(), "rb");
408
409 if (fp != NULL) {
410 fclose(fp);
411 // File found
412 // returning an allocated array here for backwards compatibility reasons
413 char *file_path = reinterpret_cast<char *>(malloc(path.length() + 1));
414 STRCPY(file_path, path.length() + 1, path.c_str());
415 return file_path;
416 }
417
418 if (fp) {
419 fclose(fp);
420 }
421 }
422
423 // File not found
424 printf("\nerror: sdkFindFilePath: file <%s> not found!\n", filename);
425 return 0;
426}
427
428#endif // COMMON_HELPER_STRING_H_
#define T
Definition exp.cpp:237
bool getCmdLineArgumentString(const int argc, const char **argv, const char *string_ref, char **string_retval)
Definition helper_string.h:244
char * sdkFindFilePath(const char *filename, const char *executable_path)
Definition helper_string.h:278
float getCmdLineArgumentFloat(const int argc, const char **argv, const char *string_ref)
Definition helper_string.h:212
int getFileExtension(char *filename, char **extension)
Definition helper_string.h:110
#define STRCPY(sFilePath, nLength, sPath)
Definition helper_string.h:74
bool getCmdLineArgumentValue(const int argc, const char **argv, const char *string_ref, T *value)
Definition helper_string.h:155
#define FOPEN(fHandle, filename, mode)
Definition helper_string.h:78
#define STRNCASECMP
Definition helper_string.h:71
bool checkCmdLineFlag(const int argc, const char **argv, const char *string_ref)
Definition helper_string.h:127
int stringRemoveDelimiter(char delimiter, const char *string)
Definition helper_string.h:96
int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref)
Definition helper_string.h:180