TransferBench.hpp 7.54 KB
Newer Older
Gilbert Lee's avatar
Gilbert Lee committed
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
/*
Copyright (c) 2019-2022 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <vector>
#include <sstream>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <set>
#include <unistd.h>
#include <map>
#include <iostream>
#include <sstream>
#include <hip/hip_runtime.h>
#include <hip/hip_ext.h>
#include <hsa/hsa_ext_amd.h>

#include "EnvVars.hpp"

// Helper macro for catching HIP errors
#define HIP_CALL(cmd)                                                   \
    do {                                                                \
        hipError_t error = (cmd);                                       \
        if (error != hipSuccess)                                        \
        {                                                               \
            std::cerr << "Encountered HIP error (" << hipGetErrorString(error) << ") at line " \
                      << __LINE__ << " in file " << __FILE__ << "\n";   \
            exit(-1);                                                   \
        }                                                               \
    } while (0)

// Simple configuration parameters
Gilbert Lee's avatar
Gilbert Lee committed
53
size_t const DEFAULT_BYTES_PER_TRANSFER = (1<<26);  // Amount of data transferred per Transfer
Gilbert Lee's avatar
Gilbert Lee committed
54
55
56
57

// Different src/dst memory types supported
typedef enum
{
gilbertlee-amd's avatar
gilbertlee-amd committed
58
59
60
61
62
  MEM_CPU          = 0, // Coarse-grained pinned CPU memory
  MEM_GPU          = 1, // Coarse-grained global GPU memory
  MEM_CPU_FINE     = 2, // Fine-grained pinned CPU memory
  MEM_GPU_FINE     = 3, // Fine-grained global GPU memory
  MEM_CPU_UNPINNED = 4 // Unpinned CPU memory
Gilbert Lee's avatar
Gilbert Lee committed
63
64
} MemType;

Gilbert Lee's avatar
Gilbert Lee committed
65
66
67
68
bool IsGpuType(MemType m)
{
  return (m == MEM_GPU || m == MEM_GPU_FINE);
}
gilbertlee-amd's avatar
gilbertlee-amd committed
69
70
71
72
bool IsCpuType(MemType m)
{
  return (m == MEM_CPU || m == MEM_CPU_FINE || m == MEM_CPU_UNPINNED);
}
Gilbert Lee's avatar
Gilbert Lee committed
73

gilbertlee-amd's avatar
gilbertlee-amd committed
74
char const MemTypeStr[6] = "CGBFU";
Gilbert Lee's avatar
Gilbert Lee committed
75

Gilbert Lee's avatar
Gilbert Lee committed
76
77
78
79
80
81
82
83
MemType inline CharToMemType(char const c)
{
  switch (c)
  {
  case 'C': return MEM_CPU;
  case 'G': return MEM_GPU;
  case 'B': return MEM_CPU_FINE;
  case 'F': return MEM_GPU_FINE;
gilbertlee-amd's avatar
gilbertlee-amd committed
84
  case 'U': return MEM_CPU_UNPINNED;
Gilbert Lee's avatar
Gilbert Lee committed
85
86
87
88
89
90
  default:
    printf("[ERROR] Unexpected mem type (%c)\n", c);
    exit(1);
  }
}

Gilbert Lee's avatar
Gilbert Lee committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
typedef enum
{
  MODE_FILL  = 0,         // Fill data with pattern
  MODE_CHECK = 1          // Check data against pattern
} ModeType;

// Each threadblock copies N floats from src to dst
struct BlockParam
{
  int       N;
  float*    src;
  float*    dst;
  long long startCycle;
  long long stopCycle;
};

Gilbert Lee's avatar
Gilbert Lee committed
107
108
// Each Transfer is a uni-direction operation from a src memory to dst memory
struct Transfer
Gilbert Lee's avatar
Gilbert Lee committed
109
{
Gilbert Lee's avatar
Gilbert Lee committed
110
  int     transferIndex;       // Transfer identifier
Gilbert Lee's avatar
Gilbert Lee committed
111

Gilbert Lee's avatar
Gilbert Lee committed
112
113
  // Transfer config
  MemType exeMemType;          // Transfer executor type (CPU or GPU)
Gilbert Lee's avatar
Gilbert Lee committed
114
115
116
117
118
  int     exeIndex;            // Executor index (NUMA node for CPU / device ID for GPU)
  MemType srcMemType;          // Source memory type
  int     srcIndex;            // Source device index
  MemType dstMemType;          // Destination memory type
  int     dstIndex;            // Destination device index
Gilbert Lee's avatar
Gilbert Lee committed
119
  int     numBlocksToUse;      // Number of threadblocks to use for this Transfer
120
  size_t  numBytes;            // Number of bytes to Transfer
gilbertlee-amd's avatar
gilbertlee-amd committed
121
  size_t  numBytesToCopy;      // Number of bytes to copy
Gilbert Lee's avatar
Gilbert Lee committed
122
123
124
125
126
127
128
129
130
131

  // Memory
  float*  srcMem;              // Source memory
  float*  dstMem;              // Destination memory

  // How memory is split across threadblocks / CPU cores
  std::vector<BlockParam> blockParam;
  BlockParam* blockParamGpuPtr;

  // Results
Gilbert Lee's avatar
Gilbert Lee committed
132
  double  transferTime;
Gilbert Lee's avatar
Gilbert Lee committed
133
134
135
136
137
138
139
140
141

  // Prepares src memory and how to divide N elements across threadblocks/threads
  void PrepareBlockParams(EnvVars const& ev, size_t const N);
};

typedef std::pair<MemType, int> Executor;

struct ExecutorInfo
{
gilbertlee-amd's avatar
gilbertlee-amd committed
142
  std::vector<Transfer*>   transfers;     // Transfers to execute
143
  size_t                   totalBytes;    // Total bytes this executor transfers
Gilbert Lee's avatar
Gilbert Lee committed
144
145
146
147
148
149
150
151
152
153
154
155

  // For GPU-Executors
  int                      totalBlocks;   // Total number of CUs/CPU threads to use
  BlockParam*              blockParamGpu; // Copy of block parameters in GPU device memory
  std::vector<hipStream_t> streams;
  std::vector<hipEvent_t>  startEvents;
  std::vector<hipEvent_t>  stopEvents;

  // Results
  double totalTime;
};

Gilbert Lee's avatar
Gilbert Lee committed
156
typedef std::map<Executor, ExecutorInfo> TransferMap;
Gilbert Lee's avatar
Gilbert Lee committed
157
158
159
160
161
162
163
164

// Display usage instructions
void DisplayUsage(char const* cmdName);

// Display detected GPU topology / CPU numa nodes
void DisplayTopology(bool const outputToCsv);

// Build array of test sizes based on sampling factor
Gilbert Lee's avatar
Gilbert Lee committed
165
void PopulateTestSizes(size_t const numBytesPerTransfer, int const samplingFactor,
Gilbert Lee's avatar
Gilbert Lee committed
166
167
168
169
170
                       std::vector<size_t>& valuesofN);

void ParseMemType(std::string const& token, int const numCpus, int const numGpus,
                  MemType* memType, int* memIndex);

Gilbert Lee's avatar
Gilbert Lee committed
171
void ParseTransfers(char* line, int numCpus, int numGpus,
Gilbert Lee's avatar
Gilbert Lee committed
172
173
                    std::vector<Transfer>& transfers);

gilbertlee-amd's avatar
gilbertlee-amd committed
174
175
void ExecuteTransfers(EnvVars const& ev, int const testNum, size_t const N,
                      std::vector<Transfer>& transfers, bool verbose = true);
Gilbert Lee's avatar
Gilbert Lee committed
176
177
178

void EnablePeerAccess(int const deviceId, int const peerDeviceId);
void AllocateMemory(MemType memType, int devIndex, size_t numBytes, void** memPtr);
gilbertlee-amd's avatar
gilbertlee-amd committed
179
void DeallocateMemory(MemType memType, void* memPtr, size_t const size = 0);
Gilbert Lee's avatar
Gilbert Lee committed
180
181
void CheckPages(char* byteArray, size_t numBytes, int targetId);
void CheckOrFill(ModeType mode, int N, bool isMemset, bool isHipCall, std::vector<float> const& fillPattern, float* ptr);
182
void RunTransfer(EnvVars const& ev, int const iteration, ExecutorInfo& exeInfo, int const transferIdx);
Gilbert Lee's avatar
Gilbert Lee committed
183
void RunPeerToPeerBenchmarks(EnvVars const& ev, size_t N, int numBlocksToUse, int readMode, int skipCpu);
gilbertlee-amd's avatar
gilbertlee-amd committed
184
void RunSweepPreset(EnvVars const& ev, size_t const numBytesPerTransfer, int const numBlocksToUse, bool const isRandom);
Gilbert Lee's avatar
Gilbert Lee committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

// Return the maximum bandwidth measured for given (src/dst) pair
double GetPeakBandwidth(EnvVars const& ev,
                        size_t  const  N,
                        int     const  isBidirectional,
                        int     const  readMode,
                        int     const  numBlocksToUse,
                        MemType const  srcMemType,
                        int     const  srcIndex,
                        MemType const  dstMemType,
                        int     const  dstIndex);

std::string GetLinkTypeDesc(uint32_t linkType, uint32_t hopCount);
std::string GetDesc(MemType srcMemType, int srcIndex,
                    MemType dstMemType, int dstIndex);
Gilbert Lee's avatar
Gilbert Lee committed
200
std::string GetTransferDesc(Transfer const& transfer);
Gilbert Lee's avatar
Gilbert Lee committed
201
202
int RemappedIndex(int const origIdx, MemType const memType);
int GetWallClockRate(int deviceId);
gilbertlee-amd's avatar
gilbertlee-amd committed
203
void LogTransfers(FILE *fp, int const testNum, std::vector<Transfer> const& transfers);