You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  1. /*
  2. * This is component of StoRM - Cuda Plugin to check whether type alignment matches the assumptions done while optimizing the code.
  3. */
  4. #include <cstdint>
  5. #include <utility>
  6. #include <vector>
  7. #define CONTAINER_SIZE 100ul
  8. template <typename IndexType, typename ValueType>
  9. int checkForAlignmentOfPairTypes(size_t containerSize, IndexType const firstValue, ValueType const secondValue) {
  10. std::vector<std::pair<IndexType, ValueType>>* myVector = new std::vector<std::pair<IndexType, ValueType>>();
  11. for (size_t i = 0; i < containerSize; ++i) {
  12. myVector->push_back(std::make_pair(firstValue, secondValue));
  13. }
  14. size_t myVectorSize = myVector->size();
  15. IndexType* firstStart = &(myVector->at(0).first);
  16. IndexType* firstEnd = &(myVector->at(myVectorSize - 1).first);
  17. ValueType* secondStart = &(myVector->at(0).second);
  18. ValueType* secondEnd = &(myVector->at(myVectorSize - 1).second);
  19. size_t startOffset = reinterpret_cast<size_t>(secondStart) - reinterpret_cast<size_t>(firstStart);
  20. size_t endOffset = reinterpret_cast<size_t>(secondEnd) - reinterpret_cast<size_t>(firstEnd);
  21. size_t firstOffset = reinterpret_cast<size_t>(firstEnd) - reinterpret_cast<size_t>(firstStart);
  22. size_t secondOffset = reinterpret_cast<size_t>(secondEnd) - reinterpret_cast<size_t>(secondStart);
  23. delete myVector;
  24. myVector = nullptr;
  25. if (myVectorSize != containerSize) {
  26. return -2;
  27. }
  28. // Check for alignment:
  29. // Requirement is that the pairs are aligned like: first, second, first, second, first, second, ...
  30. if (sizeof(IndexType) != sizeof(ValueType)) {
  31. return -3;
  32. }
  33. if (startOffset != sizeof(IndexType)) {
  34. return -4;
  35. }
  36. if (endOffset != sizeof(IndexType)) {
  37. return -5;
  38. }
  39. if (firstOffset != ((sizeof(IndexType) + sizeof(ValueType)) * (myVectorSize - 1))) {
  40. return -6;
  41. }
  42. if (secondOffset != ((sizeof(IndexType) + sizeof(ValueType)) * (myVectorSize - 1))) {
  43. return -7;
  44. }
  45. return 0;
  46. }
  47. int main(int argc, char* argv[]) {
  48. int result = 0;
  49. result = checkForAlignmentOfPairTypes<uint_fast64_t, double>(CONTAINER_SIZE, 42, 3.14);
  50. if (result != 0) {
  51. return result;
  52. }
  53. return 0;
  54. }