utils.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /****************************************************************************\
  2. * Copyright (c) 2011, Advanced Micro Devices, Inc. *
  3. * All rights reserved. *
  4. * *
  5. * Redistribution and use in source and binary forms, with or without *
  6. * modification, are permitted provided that the following conditions *
  7. * are met: *
  8. * *
  9. * Redistributions of source code must retain the above copyright notice, *
  10. * this list of conditions and the following disclaimer. *
  11. * *
  12. * Redistributions in binary form must reproduce the above copyright notice, *
  13. * this list of conditions and the following disclaimer in the documentation *
  14. * and/or other materials provided with the distribution. *
  15. * *
  16. * Neither the name of the copyright holder nor the names of its contributors *
  17. * may be used to endorse or promote products derived from this software *
  18. * without specific prior written permission. *
  19. * *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR *
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR *
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  31. * *
  32. * If you use the software (in whole or in part), you shall adhere to all *
  33. * applicable U.S., European, and other export laws, including but not *
  34. * limited to the U.S. Export Administration Regulations (“EAR”), (15 C.F.R. *
  35. * Sections 730 through 774), and E.U. Council Regulation (EC) No 1334/2000 *
  36. * of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, you *
  37. * hereby certify that, except pursuant to a license granted by the United *
  38. * States Department of Commerce Bureau of Industry and Security or as *
  39. * otherwise permitted pursuant to a License Exception under the U.S. Export *
  40. * Administration Regulations ("EAR"), you will not (1) export, re-export or *
  41. * release to a national of a country in Country Groups D:1, E:1 or E:2 any *
  42. * restricted technology, software, or source code you receive hereunder, *
  43. * or (2) export to Country Groups D:1, E:1 or E:2 the direct product of such *
  44. * technology or software, if such foreign produced direct product is subject *
  45. * to national security controls as identified on the Commerce Control List *
  46. *(currently found in Supplement 1 to Part 774 of EAR). For the most current *
  47. * Country Group listings, or for additional information about the EAR or *
  48. * your obligations under those regulations, please refer to the U.S. Bureau *
  49. * of Industry and Security’s website at http://www.bis.doc.gov/. *
  50. \****************************************************************************/
  51. #include <stdio.h>
  52. #include <sys/stat.h>
  53. #include <string.h>
  54. #include <stdlib.h>
  55. #include "utils.h"
  56. static bool usingImages = true;
  57. //! A wrapper for malloc that checks the return value
  58. void* alloc(size_t size) {
  59. void* ptr = NULL;
  60. ptr = malloc(size);
  61. if(ptr == NULL) {
  62. perror("malloc");
  63. exit(-1);
  64. }
  65. return ptr;
  66. }
  67. // This function checks to make sure a file exists before we open it
  68. void checkFile(char* filename)
  69. {
  70. struct stat fileStatus;
  71. if(stat(filename, &fileStatus) != 0) {
  72. printf("Error opening file: %s\n", filename);
  73. exit(-1);
  74. }
  75. else {
  76. if(!(S_IFREG & fileStatus.st_mode)) {
  77. printf("File %s is not a regular file\n", filename);
  78. exit(-1);
  79. }
  80. }
  81. }
  82. // This function checks to make sure a directory exists
  83. void checkDir(char* dirpath)
  84. {
  85. struct stat fileStatus;
  86. if(stat(dirpath, &fileStatus) != 0) {
  87. printf("Directory does not exist: %s\n", dirpath);
  88. exit(-1);
  89. }
  90. else {
  91. if(!(S_IFDIR & fileStatus.st_mode)) {
  92. printf("Directory was not provided: %s\n", dirpath);
  93. exit(-1);
  94. }
  95. }
  96. }
  97. // Parse the command line arguments
  98. void parseArguments(int argc, char** argv, char** input, char** events,
  99. char** ipts, char* devicePref, bool* verifyResults)
  100. {
  101. for(int i = 2; i < argc; i++) {
  102. if(strcmp(argv[i], "-d") == 0) { // Event dump found
  103. if(i == argc-1) {
  104. printf("Usage: -e Needs directory path\n");
  105. exit(-1);
  106. }
  107. devicePref[0] = argv[i+1][0];
  108. i++;
  109. continue;
  110. }
  111. if(strcmp(argv[i], "-e") == 0) { // Event dump found
  112. if(i == argc-1) {
  113. printf("Usage: -e Needs directory path\n");
  114. exit(-1);
  115. }
  116. *events = argv[i+1];
  117. i++;
  118. continue;
  119. }
  120. if(strcmp(argv[i], "-i") == 0) { // Input found
  121. if(i == argc-1) {
  122. printf("Usage: -i Needs directory path\n");
  123. exit(-1);
  124. }
  125. *input = argv[i+1];
  126. i++;
  127. continue;
  128. }
  129. if(strcmp(argv[i], "-l") == 0) { // Ipts dump found
  130. if(i == argc-1) {
  131. printf("Usage: -l Needs directory path\n");
  132. exit(-1);
  133. }
  134. *ipts = argv[i+1];
  135. i++;
  136. continue;
  137. }
  138. if(strcmp(argv[i], "-n") == 0) { // Don't use OpenCL images
  139. setUsingImages(false);
  140. continue;
  141. }
  142. if(strcmp(argv[i], "-v") == 0) { // Verify results
  143. *verifyResults = true;
  144. continue;
  145. }
  146. }
  147. }
  148. // This function that takes a positive integer 'value' and returns
  149. // the nearest multiple of 'multiple' (used for padding columns)
  150. unsigned int roundUp(unsigned int value, unsigned int multiple) {
  151. unsigned int remainder = value % multiple;
  152. // Make the value a multiple of multiple
  153. if(remainder != 0) {
  154. value += (multiple-remainder);
  155. }
  156. return value;
  157. }
  158. // Concatenate two strings and return a pointer to the new string
  159. char* smartStrcat(char* str1, char* str2)
  160. {
  161. char* newStr = NULL;
  162. newStr = (char*)alloc((strlen(str1)+strlen(str2)+1)*sizeof(char));
  163. strcpy(newStr, str1);
  164. strcat(newStr, str2);
  165. return newStr;
  166. }
  167. // Set the value of using images to true if they are being
  168. // used, or false if they are not
  169. void setUsingImages(bool val)
  170. {
  171. usingImages = val;
  172. }
  173. // Return whether or not images are being used
  174. bool isUsingImages()
  175. {
  176. return usingImages;
  177. }