common.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /******************************************************************************
  2. * Copyright 2020 The Apollo Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *****************************************************************************/
  16. /*
  17. * Copyright 2018-2019 Autoware Foundation. All rights reserved.
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the "License");
  20. * you may not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * http://www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an "AS IS" BASIS,
  27. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. */
  31. /**
  32. * @author Kosuke Murakami
  33. * @date 2019/02/26
  34. */
  35. /**
  36. * @author Yan haixu
  37. * Contact: just github.com/hova88
  38. * @date 2021/04/30
  39. */
  40. #pragma once
  41. // headers in STL
  42. #include <stdio.h>
  43. #include <assert.h>
  44. #include <iostream>
  45. #include <fstream>
  46. #include <string>
  47. #include <vector>
  48. // headers in CUDA
  49. #include "cuda_runtime_api.h"
  50. using namespace std;
  51. // using MACRO to allocate memory inside CUDA kernel
  52. #define NUM_3D_BOX_CORNERS_MACRO 8
  53. #define NUM_2D_BOX_CORNERS_MACRO 4
  54. #define NUM_THREADS_MACRO 64
  55. // need to be changed when num_threads_ is changed
  56. #define DIVUP(m, n) ((m) / (n) + ((m) % (n) > 0))
  57. #define GPU_CHECK(ans) \
  58. { \
  59. GPUAssert((ans), __FILE__, __LINE__); \
  60. }
  61. inline void GPUAssert(cudaError_t code, const char *file, int line,
  62. bool abort = true)
  63. {
  64. if (code != cudaSuccess)
  65. {
  66. fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file,
  67. line);
  68. if (abort)
  69. exit(code);
  70. }
  71. };
  72. template <typename T>
  73. void HOST_SAVE(T *array, int size, string filename, string root = "../test/result", string postfix = ".txt")
  74. {
  75. string filepath = root + "/" + filename + postfix;
  76. if (postfix == ".bin")
  77. {
  78. fstream file(filepath, ios::out | ios::binary);
  79. file.write(reinterpret_cast<char *>(array), sizeof(size * sizeof(T)));
  80. file.close();
  81. std::cout << "|>>>| Data has been written in " << filepath << " |<<<|" << std::endl;
  82. return;
  83. }
  84. else if (postfix == ".txt")
  85. {
  86. ofstream file(filepath, ios::out);
  87. for (int i = 0; i < size; ++i)
  88. file << array[i] << " ";
  89. file.close();
  90. std::cout << "|>>>| Data has been written in " << filepath << " |<<<|" << std::endl;
  91. return;
  92. }
  93. };
  94. template <typename T>
  95. void DEVICE_SAVE(T *array, int size, string filename, string root = "../test/result", string postfix = ".txt")
  96. {
  97. T *temp_ = new T[size];
  98. cudaMemcpy(temp_, array, size * sizeof(T), cudaMemcpyDeviceToHost);
  99. HOST_SAVE<T>(temp_, size, filename, root, postfix);
  100. delete[] temp_;
  101. };
  102. // int TXTtoArrary( float* &points_array , string file_name , int num_feature = 4)
  103. // {
  104. // ifstream InFile;
  105. // InFile.open(file_name.data());
  106. // assert(InFile.is_open());
  107. // vector<float> temp_points;
  108. // string c;
  109. // while (!InFile.eof())
  110. // {
  111. // InFile >> c;
  112. // temp_points.push_back(atof(c.c_str()));
  113. // }
  114. // points_array = new float[temp_points.size()];
  115. // for (int i = 0 ; i < temp_points.size() ; ++i) {
  116. // points_array[i] = temp_points[i];
  117. // }
  118. // InFile.close();
  119. // return temp_points.size() / num_feature;
  120. // };