rocm_bandwidth_test.hpp 17.2 KB
Newer Older
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
////////////////////////////////////////////////////////////////////////////////
//
// The University of Illinois/NCSA
// Open Source License (NCSA)
// 
// Copyright (c) 2014-2015, Advanced Micro Devices, Inc. All rights reserved.
// 
// Developed by:
// 
//                 AMD Research and AMD HSA Software Development
// 
//                 Advanced Micro Devices, Inc.
// 
//                 www.amd.com
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal with 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:
// 
//  - Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimers.
//  - Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimers in
//    the documentation and/or other materials provided with the distribution.
//  - Neither the names of Advanced Micro Devices, Inc,
//    nor the names of its contributors may be used to endorse or promote
//    products derived from this Software without specific prior written
//    permission.
// 
// 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 CONTRIBUTORS 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 WITH THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef __ROC_BANDWIDTH_TEST_H__
#define __ROC_BANDWIDTH_TEST_H__

Ashutosh Mishra's avatar
Ashutosh Mishra committed
46
#include "hsa.h"
47
48
#include "base_test.hpp"
#include "common.hpp"
49

50
#include <vector>
51
#include <chrono>
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

using namespace std;

// Structure to encapsulate a RocR agent and its index in a list
typedef struct agent_info {

  agent_info(hsa_agent_t agent,
             uint32_t index, hsa_device_type_t device_type) {
    agent_ = agent;
    index_ = index;
    device_type_ = device_type;
  }

  agent_info() {}
  
  uint32_t index_;
  hsa_agent_t agent_;
  hsa_device_type_t device_type_;
70
  char name_[64];     // Size specified in public header file
71
  char uuid_[24];     // Unique ID of the device
72
  char bdf_id_[16];   // Bus (8-bits), Device (5-bits), Function (3-bits)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

} agent_info_t;

typedef struct pool_info {

  pool_info(hsa_agent_t agent, uint32_t agent_index,
            hsa_amd_memory_pool_t pool, hsa_amd_segment_t segment,
            size_t size, uint32_t index, bool is_fine_grained,
            bool is_kernarg, bool access_to_all,
            hsa_amd_memory_pool_access_t owner_access) {

    pool_ = pool;
    index_ = index;
    segment_ = segment;
    owner_agent_ = agent;
    agent_index_ = agent_index;
    allocable_size_ = size;
    is_kernarg_ = is_kernarg;
    owner_access_ = owner_access;
    access_to_all_ = access_to_all;
    is_fine_grained_ = is_fine_grained;
  }

  pool_info() {}

  uint32_t index_;
  bool is_kernarg_;
  bool access_to_all_;
  bool is_fine_grained_;
  size_t allocable_size_;
  uint32_t agent_index_;
  hsa_agent_t owner_agent_;
  hsa_amd_segment_t segment_;
  hsa_amd_memory_pool_t pool_;
  hsa_amd_memory_pool_access_t owner_access_;

} pool_info_t;

// Used to print out topology info
typedef struct agent_pool_info {

  agent_pool_info() {}
  
  agent_info agent;
  
  vector<pool_info_t> pool_list;

} agent_pool_info_t;

typedef struct async_trans {

  uint32_t req_type_;
  union {
    struct {
      bool bidir_;
      bool uses_gpu_;
      uint32_t src_idx_;
      uint32_t dst_idx_;
      hsa_amd_memory_pool_t src_pool_;
      hsa_amd_memory_pool_t dst_pool_;
    } copy;
    struct {
      void* code_;
      uint32_t agent_idx_;
      hsa_agent_t agent_;
      uint32_t pool_idx_;
      hsa_amd_memory_pool_t pool_;
    } kernel;
  };

  // Cpu BenchMark average copy time
  vector<double> cpu_avg_time_;

  // Cpu Min time
  vector<double> cpu_min_time_;

  // Gpu BenchMark average copy time
  vector<double> gpu_avg_time_;

  // Gpu Min time
  vector<double> gpu_min_time_;

  // BenchMark's Average copy time and average bandwidth
  vector<double> avg_time_;
  vector<double> avg_bandwidth_;

  // BenchMark's Min copy time and peak bandwidth
  vector<double> min_time_;
  vector<double> peak_bandwidth_;

  async_trans(uint32_t req_type) { req_type_ = req_type; }
} async_trans_t;

typedef enum Request_Type {

  REQ_READ = 1,
  REQ_WRITE = 2,
170
171
  REQ_VERSION = 3,
  REQ_TOPOLOGY = 4,
172
173
174
175
176
177
178
179
  REQ_LIST_DEVS = 5,
  REQ_COPY_BIDIR = 6,
  REQ_COPY_UNIDIR = 7,
  REQ_COPY_ALL_BIDIR = 8,
  REQ_COPY_ALL_UNIDIR = 9,
  REQ_CONCURRENT_COPY_BIDIR = 10,
  REQ_CONCURRENT_COPY_UNIDIR = 11,
  REQ_INVALID = 12,
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

} Request_Type;

class RocmBandwidthTest : public BaseTest {

 public:

  // @brief: Constructor for test case of RocmBandwidthTest
  RocmBandwidthTest(int argc, char** argv);

  // @brief: Destructor for test case of RocmBandwidthTest
  virtual ~RocmBandwidthTest();

  // @brief: Setup the environment for measurement
  virtual void SetUp();

  // @brief: Core measurement execution
  virtual void Run();

  // @brief: Clean up and retrive the resource
  virtual void Close();

  // @brief: Display the results
  virtual void Display() const;

205
206
207
  // @brief: Return exit value, useful in case of error
  int32_t GetExitValue() { return exit_value_; }

208
209
210
211
212
213
214
215
 private:

  // @brief: Print Help Menu Screen
  void PrintHelpScreen();

  // @brief: Discover the topology of pools on Rocm Platform
  void DiscoverTopology();

216
217
218
  // @brief: Populate link properties for the set of agents
  void DiscoverLinkProps();
  void BindLinkProps(uint32_t idx1, uint32_t idx2);
219
220
221
222
223
224
225

  // @brief: Populates the access matrix
  void PopulateAccessMatrix();

  // @brief: Print topology info
  void PrintTopology();

226
227
228
229
  // @brief: Print in matrix form various
  // properties such as access, link weight,
  // link type and number of hops, etc
  void PrintLinkPropsMatrix(uint32_t key) const;
230
231
232
233
234
235
236
237
238
239
240

  // @brief: Print info on agents in system
  void PrintAgentsList();

  // @brief: Print info on memory pools in system
  void PrintPoolsList();

  // @brief: Parse the arguments provided by user to
  // build list of transactions
  void ParseArguments();
  
241
242
243
244
  // @brief Validate user input of primary operations
  void ValidateInputFlags(uint32_t pf_cnt,
                          uint32_t copy_mask, uint32_t copy_ctrl_mask);
  
245
246
247
248
249
250
251
252
253
  // @brief: Print the list of transactions
  void PrintTransList();

  // @brief: Run read/write requests of users
  void RunIOBenchmark(async_trans_t& trans);

  // @brief: Run copy requests of users
  void RunCopyBenchmark(async_trans_t& trans);

254
255
256
257
  // @brief: Run copy requests of users
  void RunConcurrentCopyBenchmark(bool bidir,
                                  vector<async_trans_t>& trans_list);

258
259
260
261
  // @brief: Get iteration number
  uint32_t GetIterationNum();

  // @brief: Get the mean copy time
262
  double GetMeanTime(vector<double>& vec);
263
264

  // @brief: Get the min copy time
265
  double GetMinTime(vector<double>& vec);
266
267

  // @brief: Dispaly Benchmark result
268
269
  void PopulatePerfMatrix(bool peak, double* perf_matrix) const;
  void PrintPerfMatrix(bool validate, bool peak, double* perf_matrix) const;
270
271
272
273
  void DisplayDevInfo() const;
  void DisplayIOTime(async_trans_t& trans) const;
  void DisplayCopyTime(async_trans_t& trans) const;
  void DisplayCopyTimeMatrix(bool peak) const;
274
  void DisplayValidationMatrix() const;
275
276
 
 private:
277
278
279
280
281

  // @brief: Validate the arguments passed in by user
  bool ValidateArguments();
  bool ValidateReadReq();
  bool ValidateWriteReq();
282
  bool ValidateReadOrWriteReq(vector<size_t>& in_list);
283
284
285
286
287

  void ValidateCopyBidirFlags(uint32_t copy_ctrl_mask);
  void ValidateCopyAllBidirFlags(uint32_t copy_ctrl_mask);
  void ValidateCopyAllUnidirFlags(uint32_t copy_ctrl_mask);
  void ValidateCopyUnidirFlags(uint32_t copy_mask, uint32_t copy_ctrl_mask);
288
289
290
  
  bool ValidateBidirCopyReq();
  bool ValidateUnidirCopyReq();
291
  bool ValidateConcurrentCopyReq();
292
  bool ValidateCopyReq(vector<size_t>& in_list);
293
294
295
  void PrintIOAccessError(uint32_t agent_idx, uint32_t pool_idx);
  void PrintCopyAccessError(uint32_t src_pool_idx, uint32_t dst_pool_idx);
  
296
297
  bool PoolIsPresent(vector<size_t>& in_list);
  bool PoolIsDuplicated(vector<size_t>& in_list);
298
299
300

  // @brief: Builds a list of transaction per user request
  void ComputeCopyTime(async_trans_t& trans);
301
  void ComputeCopyTime(vector<async_trans_t>& trans_list);
302
303
  void BuildDeviceList();
  void BuildBufferList();
304
305
306
307
308
309
310
311
  bool BuildTransList();
  bool BuildReadTrans();
  bool BuildWriteTrans();
  bool BuildBidirCopyTrans();
  bool BuildUnidirCopyTrans();
  bool BuildAllPoolsBidirCopyTrans();
  bool BuildAllPoolsUnidirCopyTrans();
  bool BuildReadOrWriteTrans(uint32_t req_type,
312
                             vector<size_t>& in_list);
313
  bool BuildCopyTrans(uint32_t req_type,
314
315
                      vector<size_t>& src_list,
                      vector<size_t>& dst_list);
316
317
  bool BuildConcurrentCopyTrans(uint32_t req_type,
                                vector<size_t>& dev_list);
318

319
320
  void WaitForCopyCompletion(vector<hsa_signal_t>& signal_list);

321
  void AllocateCopyBuffers(size_t size,
322
                           void*& src, hsa_amd_memory_pool_t src_pool,
323
324
                           void*& dst, hsa_amd_memory_pool_t dst_pool);
  
325
326
327
328
329
330
331
332
333
334
  void AllocateConcurrentCopyResources(bool bidir,
                                       vector<async_trans_t>& trans_list,
                                       vector<void*>& buffer_list,
                                       vector<hsa_agent_t>& dev_list,
                                       vector<uint32_t>& dev_idx_list,
                                       vector<hsa_signal_t>& sig_list,
                                       vector<hsa_amd_memory_pool_t>& pool_list);
  
  void ReleaseBuffers(vector<void*>& buffer_list);
  void ReleaseSignals(vector<hsa_signal_t>& signal_list);
335
  
336
  double GetGpuCopyTime(bool bidir, hsa_signal_t signal_fwd, hsa_signal_t signal_rev);
337
338
339
340
341
342
343
  
  void InitializeSrcBuffer(size_t size, void* buf_cpy,
                           uint32_t cpy_dev_idx, hsa_agent_t cpy_agent);
  
  bool ValidateDstBuffer(size_t max_size, size_t curr_size,
                         void* buf_cpy, uint32_t cpy_dev_idx, hsa_agent_t cpy_agent);

344
345
346
347
348
349
350
  void copy_buffer(void* dst, hsa_agent_t dst_agent,
                   void* src, hsa_agent_t src_agent,
                   size_t size, hsa_signal_t signal);
  bool FilterCpuPool(uint32_t req_type,
                     hsa_device_type_t dev_type,
                     bool fine_grained);

351
  // Find the mirror transaction if present
352
  bool FindMirrorRequest(bool reverse, uint32_t src_idx, uint32_t dst_idx);
353

354
355
356
357
358
359
360
361
362
363
  // @brief: Check if agent and access memory pool, if so, set 
  // access to the agent, if not, exit
  void AcquireAccess(hsa_agent_t agent, void* ptr);
  void AcquirePoolAcceses(uint32_t src_dev_idx, hsa_agent_t src_agent, void* src,
                          uint32_t dst_dev_idx, hsa_agent_t dst_agent, void* dst);

  // Functions to find agents and memory pools and udpate
  // relevant data structures used to maintain system topology
  friend hsa_status_t AgentInfo(hsa_agent_t agent, void* data);
  friend hsa_status_t MemPoolInfo(hsa_amd_memory_pool_t pool, void* data);
364
365
366
  
  // Populate the Bus Device Function of Gpu device
  friend void PopulateBDF(uint32_t bdf_id, agent_info_t *agent_info);
367

368
369
370
371
372
373
374
375
376
  // Compute the type and weight of a link
  friend uint32_t GetLinkType(hsa_device_type_t src_dev_type,
                     hsa_device_type_t dst_dev_type,
                     hsa_amd_memory_pool_link_info_t* link_info, uint32_t hops);
  friend uint32_t GetLinkWeight(hsa_amd_memory_pool_link_info_t* link_info, uint32_t hops);

  // Return value of input key as string
  friend std::string GetValueAsString(uint32_t key, uint32_t value);

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  // Structure of Version used to identify an instance of RocmBandwidthTest
  struct RocmBandwidthVersion {

    // Tracks changes such as re-design
    // re-factor, re-write, extend with
    // new major functionality, etc
    uint32_t major_id;

    // Tracks changes that affect Apis
    // being added, removed, modified
    uint32_t minor_id;

    // Tracks changes that affect Apis
    // being added, removed, modified
    uint32_t step_id;

    // Used to pack space for structure alignment
    uint32_t reserved;
  };
396

397
398
  RocmBandwidthVersion version_;
  void PrintVersion() const;
Ramesh Errabolu's avatar
Ramesh Errabolu committed
399
  void PrintLaunchCmd() const;
400
401
  std::string GetVersion() const;

402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  // Used to help count agent_info
  uint32_t agent_index_;

  // List used to store agent info, indexed by agent_index_
  vector<agent_info_t> agent_list_;

  // Used to help count pool_info_t
  uint32_t pool_index_;

  // List used to store pool_info_t, indexed by pool_index_
  vector<pool_info_t> pool_list_;

  // List used to store agent_pool_info_t
  vector<agent_pool_info_t> agent_pool_list_;

  // List of agents involved in a bidrectional copy operation
  // Size of the list cannot exceed the number of agents
  // reported by the system
420
  vector<size_t> bidir_list_;
421
422
423
424

  // List of source agents in a unidrectional copy operation
  // Size of the list cannot exceed the number of agents
  // reported by the system
425
  vector<size_t> src_list_;
426
427
428
429

  // List of destination agents in a unidrectional copy operation
  // Size of the list cannot exceed the number of agents
  // reported by the system
430
  vector<size_t> dst_list_;
431
432
433
434

  // List of agents involved in read operation. Has
  // two agents, the first agent hosts the memory pool
  // while the second agent executes the read operation
435
  vector<size_t> read_list_;
436
437
438
439
  
  // List of agents involved in write operation. Has
  // two agents, the first agent hosts the memory pool
  // while the second agent executes the write operation
440
  vector<size_t> write_list_;
441
442
443
  
  // List of sizes to use in copy and read/write transactions
  // Size is specified in terms of Megabytes
444
  vector<size_t> size_list_;
445
446
447
448

  // Type of service requested by user
  uint32_t req_read_;
  uint32_t req_write_;
449
450
  uint32_t req_version_;
  uint32_t req_topology_;
451
  uint32_t req_list_devs_;
452
453
454
455
  uint32_t req_copy_bidir_;
  uint32_t req_copy_unidir_;
  uint32_t req_copy_all_bidir_;
  uint32_t req_copy_all_unidir_;
456
457
  uint32_t req_concurrent_copy_bidir_;
  uint32_t req_concurrent_copy_unidir_;
458
459
460
461
462
  
  static const uint32_t USR_SRC_FLAG = 0x01;
  static const uint32_t USR_DST_FLAG = 0x02;

  static const uint32_t USR_BUFFER_SIZE = 0x01;
463
464
465
466
  static const uint32_t USR_BUFFER_INIT = 0x02;
  static const uint32_t CPU_VISIBLE_TIME = 0x04;
  static const uint32_t DEV_COPY_LATENCY = 0x08;
  static const uint32_t VALIDATE_COPY_OP = 0x010;
467

468
469
470
  static const uint32_t LINK_TYPE_SELF = 0x00;
  static const uint32_t LINK_TYPE_PCIE = 0x01;
  static const uint32_t LINK_TYPE_XGMI = 0x02;
471
  static const uint32_t LINK_TYPE_IGNORED = 0x03;
472
473
  static const uint32_t LINK_TYPE_NO_PATH = 0xFFFFFFFF;

474
475
476
477
  static const uint32_t LINK_PROP_HOPS = 0x00;
  static const uint32_t LINK_PROP_TYPE = 0x01;
  static const uint32_t LINK_PROP_WEIGHT = 0x02;
  static const uint32_t LINK_PROP_ACCESS = 0x03;
478
479
  
  // Encodes validation failure
480
  static const double VALIDATE_COPY_OP_FAILURE;
481

482
483
484
485
486
487
488
489
  // List used to store transactions per user request
  vector<async_trans_t> trans_list_;

  // List used to track agents involved in various transactions
  uint32_t* active_agents_list_;

  // Matrix used to track Access among agents
  uint32_t* access_matrix_;
490
  uint32_t* link_hops_matrix_;
491
492
  uint32_t* link_type_matrix_;
  uint32_t* link_weight_matrix_;
493
  uint32_t* direct_access_matrix_;
494
495
496
  
  // Env key to determine if Fine-grained or
  // Coarse-grained pool should be filtered out
497
498
  char* skip_cpu_fine_grain_;
  char* skip_gpu_coarse_grain_;
499
500
501
502
503
504
505
  
  // Env key to determine if the run should block
  // or actively wait on completion signal
  char* bw_blocking_run_;
  
  // Env key to determine if the run is a default one
  char* bw_default_run_;
506
507
508
  
  // Env key to specify iteration count
  char* bw_iter_cnt_;
509
510
  char* bw_sleep_time_;
  uint32_t sleep_time_;
511
512
513
514
  std::chrono::nanoseconds cpu_cp_time_;
  std::chrono::microseconds sleep_usecs_;
  std::chrono::time_point<std::chrono::steady_clock> cpu_end_;
  std::chrono::time_point<std::chrono::steady_clock> cpu_start_;
515
516
517
518
519
520
521
522
523
524

  // Variable to store argument number
  uint32_t usr_argc_;

  // Pointer to store address of argument text
  char** usr_argv_;

  // Flag to print Cpu time
  bool print_cpu_time_;

525
526
527
  // Determines if user has requested initialization
  bool init_;

528
529
  // Determines if user has requested validation
  bool validate_;
530
  long double init_val_;
531

532
533
534
535
536
  // Handles to buffer used to initialize and validate
  void* init_src_;
  void* validate_dst_;
  hsa_signal_t init_signal_;
 
537
538
539
  // Determines the latency overhead of copy operations
  bool latency_;

540
  // CPU agent used for validation
541
542
543
544
545
546
  int32_t cpu_index_;
  hsa_agent_t cpu_agent_;

  // System region
  hsa_amd_memory_pool_t sys_pool_;
 
547
548
  static const size_t SIZE_LIST[20];
  static const size_t LATENCY_SIZE_LIST[20];
549

550
551
  // Exit value to return in case of error
  int32_t exit_value_;
552
553
554
};

#endif      //  __ROC_BANDWIDTH_TEST_H__