okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
12.7 KiB501 linesraw
1//==----------- basic.hpp ----------------------------*-C++-*-------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===--------------------------------------------------------------------===//
8#ifndef __DPCT_CODEPIN_SER_BASIC_HPP__
9#define __DPCT_CODEPIN_SER_BASIC_HPP__
10
11#if defined(__linux__)
12#include <cxxabi.h>
13#endif
14#include <fstream>
15#include <iostream>
16#include <sstream>
17#include <string>
18#ifdef __NVCC__
19#include <cuda_bf16.h>
20#include <cuda_fp16.h>
21#include <cuda_runtime.h>
22#else
23#include <dpct/dpct.hpp>
24#include <sycl/sycl.hpp>
25#endif
26
27namespace dpct {
28namespace experimental {
29namespace codepin {
30
31#ifdef __NVCC__
32typedef cudaStream_t queue_t;
33#else
34typedef dpct::queue_ptr queue_t;
35#endif
36
37#ifdef __NVCC__
38typedef cudaEvent_t event_t;
39#else
40typedef sycl::event event_t;
41#endif
42
43namespace detail {
44
45inline bool is_dev_ptr(void *p) {
46#ifdef __NVCC__
47  cudaPointerAttributes attr;
48  cudaPointerGetAttributes(&attr, p);
49  if (attr.type == cudaMemoryTypeDevice)
50    return true;
51  return false;
52#else
53  dpct::pointer_attributes attributes;
54  attributes.init(p);
55  if (attributes.get_device_pointer() != nullptr)
56    return true;
57  return false;
58#endif
59}
60
61class json_stringstream {
62  public:
63  json_stringstream(std::ofstream &ofst) : os(ofst) {
64    if (!ofst.is_open()) {
65      throw std::runtime_error("Error while openning file: ");
66    }
67  }
68
69private:
70  std::string indent;
71  const size_t tab_length = 2;
72  std::ofstream &os;
73
74#if defined(__linux__)
75  const std::string newline = "\n";
76#elif defined(_WIN64)
77  const std::string newline = "\r\n";
78#else
79#error Only support windows and Linux.
80#endif
81
82
83public:
84  class json_obj {
85    bool isFirst = true;
86    json_stringstream &js;
87  private:
88    friend class json_stringstream;
89    json_obj(json_stringstream &json_ss) : js(json_ss) {
90      js.os << "{" << js.newline;
91      js.indent.append(js.tab_length, ' ');
92      js.os << js.indent;
93    }
94
95  public:
96    template<class T> T value();
97
98    void key(std::string_view key) {
99      if (!isFirst){
100        js.os << "," << js.newline << js.indent;
101      } else {
102        isFirst = false;
103      }
104      js.os << "\"" << key << "\": ";
105    };
106
107    void value(std::string_view value) { js.os << "\"" << value << "\""; };
108    void value(float value) { js.os << "\"" << value << "\""; };
109    void value(size_t value) { js.os << "\"" << value << "\""; };
110    void value(int value) { js.os << "\"" << value << "\""; };
111    void value(void *value) { js.os << "\"" << value << "\""; };
112    ~json_obj() {
113      js.indent.resize(js.indent.size() - js.tab_length);
114      js.os << js.newline;
115      js.os << js.indent;
116      js.os << "}";
117    }
118  };
119
120  class json_array {
121    bool isFirst = true;
122    json_stringstream &js;
123  public:
124    json_array(json_stringstream &json_ss) : js(json_ss) {
125      if(!(js.os))
126        return;
127      js.os << "[" << js.newline;
128      js.indent.append(js.tab_length, ' ');
129      js.os << js.indent;
130    }
131
132    json_obj object() {
133      if(!isFirst){
134        js.os << "," << js.newline << js.indent;
135      } else {
136        isFirst = false;
137      }
138      return json_obj(js);      
139    }
140
141    template<class MemberT>
142    void member(const MemberT &t) {
143      if(!isFirst){
144        js.os << "," << js.newline << js.indent;
145      } else {
146        isFirst = false;
147      }
148      js.os << t;    
149    }
150
151    ~json_array() {
152      js.indent.resize(js.indent.size() - js.tab_length);
153      js.os << js.newline;
154      js.os << js.indent;
155      js.os << "]";
156    }
157  };
158
159  template <typename T, typename = std::enable_if_t<
160                            !std::is_same_v<const char *, std::decay_t<T>>>>
161  json_stringstream &operator<<(T &&value) {
162    os << std::forward<T>(value);
163    return *this;
164  }
165
166  json_obj object(){
167    return json_obj(*this);
168  }
169  json_array array(){
170    return json_array(*this);
171  }
172};
173
174template <>
175inline json_stringstream::json_obj
176json_stringstream::json_obj::value<json_stringstream::json_obj>() {
177  return js.object();
178}
179
180template <typename T> inline std::string demangle_name() {
181  std::string ret_str = "";
182#if defined(__linux__)
183  int s;
184  auto mangle_name = typeid(T).name();
185  auto demangle_name = abi::__cxa_demangle(mangle_name, NULL, NULL, &s);
186  if (s != 0) {
187    ret_str = "CODEPIN:ERROR:0: Unable to demangle symbol " +
188              std::string(mangle_name) + ".";
189  } else {
190    ret_str = demangle_name;
191    std::free(demangle_name);
192  }
193#else
194  ret_str = typeid(T).name();
195#endif
196  return ret_str;
197}
198
199#ifdef __NVCC__
200template <> inline std::string demangle_name<__half>() { return "fp16"; }
201template <> inline std::string demangle_name<__nv_bfloat16>() { return "bf16"; }
202#else
203template <> inline std::string demangle_name<sycl::half>() { return "fp16"; }
204template <> inline std::string demangle_name<sycl::ext::oneapi::bfloat16>() {
205  return "bf16";
206}
207#endif
208
209template <class T, class T2 = void> class data_ser {
210
211public:
212  static void dump(json_stringstream &ss, T value, queue_t queue) {
213    auto obj = ss.object();
214    obj.key("Data");
215    obj.value("CODEPIN:ERROR:1: Unable to find the corresponding serialization "
216              "function.");
217  }
218  static void print_type_name(json_stringstream::json_obj &obj) {
219    obj.key("Type");
220    obj.value(std::string(demangle_name<T>()));
221  }
222};
223
224template <class T>
225class data_ser<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> {
226public:
227  static void dump(json_stringstream &ss, const T &value, queue_t queue) {
228    auto arr = ss.array();
229    arr.member<T>(value);
230  }
231  static void print_type_name(json_stringstream::json_obj &obj) {
232    obj.key("Type");
233    obj.value(std::string(demangle_name<T>()));
234  }
235};
236
237#ifdef __NVCC__
238template <> class data_ser<__half> {
239public:
240  static void dump(json_stringstream &ss, const __half &value, queue_t queue) {
241    float f = __half2float(value);
242    auto arr = ss.array();
243    arr.member<float>(value);
244  }
245  static void print_type_name(json_stringstream::json_obj &obj) {
246    obj.key("Type");
247    obj.value(std::string(demangle_name<__half>()));
248  }
249};
250template <> class data_ser<__nv_bfloat16> {
251public:
252  static void dump(json_stringstream &ss, const __nv_bfloat16 &value,
253                   queue_t queue) {
254    float f = __bfloat162float(value);
255    auto arr = ss.array();
256    arr.member<float>(value);
257  }
258  static void print_type_name(json_stringstream::json_obj &obj) {
259    obj.key("Type");
260    obj.value(std::string(demangle_name<__nv_bfloat16>()));
261  }
262};
263#else
264template <typename T>
265class data_ser<T,
266               typename std::enable_if<
267                   std::is_same<T, sycl::half>::value ||
268                   std::is_same<T, sycl::ext::oneapi::bfloat16>::value>::type> {
269public:
270  static void dump(json_stringstream &ss, const T &value, queue_t queue) {
271    auto arr = ss.array();
272    arr.member<T>(value);
273  }
274  static void print_type_name(json_stringstream::json_obj &obj) {
275    obj.key("Type");
276    obj.value(std::string(demangle_name<T>()));
277  }
278
279};
280#endif
281
282#ifdef __NVCC__
283template <> class data_ser<int3> {
284public:
285  static void dump(json_stringstream &ss, const int3 &value,
286                   queue_t queue) {
287    auto arr = ss.array();
288    {
289      auto obj_x = arr.object();
290      obj_x.key("x");
291      auto value_x =
292          obj_x
293              .value<json_stringstream::json_obj>();
294      data_ser<int>::print_type_name(value_x);
295      value_x.key("Data");
296      data_ser<int>::dump(ss, value.x, queue);
297    }
298    {
299      auto obj_y = arr.object();
300      obj_y.key("y");
301      auto value_y =
302          obj_y
303              .value<json_stringstream::json_obj>();
304      data_ser<int>::print_type_name(value_y);
305      value_y.key("Data");
306      data_ser<int>::dump(ss, value.y, queue);
307    }
308    {
309      auto obj_z = arr.object();
310      obj_z.key("z");
311      auto value_z =
312          obj_z
313              .value<json_stringstream::json_obj>();
314      data_ser<int>::print_type_name(value_z);
315      value_z.key("Data");
316      data_ser<int>::dump(ss, value.z, queue);
317    }
318  }
319  static void print_type_name(json_stringstream::json_obj &obj){
320    obj.key("Type");
321    obj.value("int3");
322  }
323};
324
325template <> class data_ser<float3> {
326public:
327  static void dump(json_stringstream &ss, const float3 &value,
328                   queue_t queue) {
329    auto arr = ss.array();
330    {
331      auto obj_x = arr.object();
332      obj_x.key("x");
333      auto value_x =
334          obj_x
335              .value<json_stringstream::json_obj>();
336      data_ser<float>::print_type_name(value_x);
337      value_x.key("Data");
338      data_ser<float>::dump(ss, value.x, queue);
339    }
340    {
341      auto obj_y = arr.object();
342      obj_y.key("y");
343      auto value_y =
344          obj_y
345              .value<json_stringstream::json_obj>();
346      data_ser<float>::print_type_name(value_y);
347      value_y.key("Data");
348      data_ser<float>::dump(ss, value.y, queue);
349    }
350    {
351      auto obj_z = arr.object();
352      obj_z.key("z");
353      auto value_z =
354          obj_z
355              .value<json_stringstream::json_obj>();
356      data_ser<float>::print_type_name(value_z);
357      value_z.key("Data");
358      data_ser<float>::dump(ss, value.z, queue);
359    }
360  }
361  static void print_type_name(json_stringstream::json_obj &obj){
362    obj.key("Type");
363    obj.value("float3");
364  }
365};
366
367#else
368template <> class data_ser<sycl::int3> {
369public:
370  static void dump(json_stringstream &ss, const sycl::int3 &value,
371                   queue_t queue) {
372    auto arr = ss.array();
373    {
374      auto obj_x = arr.object();
375      obj_x.key("x");
376      auto value_x =
377          obj_x
378              .value<json_stringstream::json_obj>();
379      data_ser<int>::print_type_name(value_x);
380      value_x.key("Data");
381      data_ser<int>::dump(ss, value.x(), queue);
382    }
383    {
384      auto obj_y = arr.object();
385      obj_y.key("y");
386      auto value_y =
387          obj_y
388              .value<json_stringstream::json_obj>();
389      data_ser<int>::print_type_name(value_y);
390      value_y.key("Data");
391      data_ser<int>::dump(ss, value.y(), queue);
392    }
393    {
394      auto obj_z = arr.object();
395      obj_z.key("z");
396      auto value_z =
397          obj_z
398              .value<json_stringstream::json_obj>();
399      data_ser<int>::print_type_name(value_z);
400      value_z.key("Data");
401      data_ser<int>::dump(ss, value.z(), queue);
402    }
403  }
404  static void print_type_name(json_stringstream::json_obj &obj){
405    obj.key("Type");
406    obj.value("sycl::int3");
407  }
408};
409
410template <> class data_ser<sycl::float3> {
411public:
412  static void dump(json_stringstream &ss, const sycl::float3 &value,
413                   queue_t queue) {
414    auto arr = ss.array();
415    {
416      auto obj_x = arr.object();
417      obj_x.key("x");
418      auto value_x =
419          obj_x
420              .value<json_stringstream::json_obj>();
421      data_ser<float>::print_type_name(value_x);
422      value_x.key("Data");
423      data_ser<float>::dump(ss, value.x(), queue);
424    }
425    {
426      auto obj_y = arr.object();
427      obj_y.key("y");
428      auto value_y =
429          obj_y
430              .value<json_stringstream::json_obj>();
431      data_ser<float>::print_type_name(value_y);
432      value_y.key("Data");
433      data_ser<float>::dump(ss, value.y(), queue);
434    }
435    {
436      auto obj_z = arr.object();
437      obj_z.key("z");
438      auto value_z =
439          obj_z
440              .value<json_stringstream::json_obj>();
441      data_ser<float>::print_type_name(value_z);
442      value_z.key("Data");
443      data_ser<float>::dump(ss, value.z(), queue);
444    }
445  }
446  static void print_type_name(json_stringstream::json_obj &obj){
447    obj.key("Type");
448    obj.value("sycl::float3");
449  }
450};
451#endif
452
453template <> class data_ser<char *> {
454public:
455  static void dump(json_stringstream &ss, const char *value,
456                   queue_t queue) {
457    auto obj = ss.object();
458    obj.key("Data");
459    const char *dump_addr = value;
460    bool is_dev = is_dev_ptr((void*)value);
461    if (is_dev) {
462      const char *h_data = new char[strlen(value)];
463#ifdef __NVCC__
464      cudaMemcpyAsync((void *)h_data, (void *)value,
465                      strlen(value) * sizeof(char), cudaMemcpyDeviceToHost,
466                      queue);
467      cudaStreamSynchronize(queue);
468#else
469      queue->memcpy((void *)h_data, (void *)value, strlen(value) * sizeof(char))
470          .wait();
471#endif
472      dump_addr = h_data;    
473    }
474    obj.value(std::string(dump_addr));
475  }
476  static void print_type_name(json_stringstream::json_obj &obj){
477    obj.key("Type");
478    obj.value("char *");
479  }
480};
481
482template <> class data_ser<std::string> {
483public:
484  static void dump(json_stringstream &ss, const std::string &value,
485                   queue_t queue) {
486    auto obj = ss.object();
487    obj.key("Data");
488    obj.value(value);
489  }
490  static void print_type_name(json_stringstream::json_obj &obj){
491    obj.key("Type");
492    obj.value("std::string");
493  }
494};
495
496} // namespace detail
497} // namespace codepin
498} // namespace experimental
499} // namespace dpct
500
501#endif