Skip to content

Commit 21d1e69

Browse files
committed
Regards #681:
* Refactored the option marshalling code, generalizing it somewhat to be usable for any aspect of CUDA which needs marshalled options * Specifically, we now have `cuda::marshalling::marshal()` and `cuda::marshalling::render()` functions which take options of some arbitrary type, and we use a partial specialization gadget where the options are actually defined, to process/parse them
1 parent f351a02 commit 21d1e69

File tree

4 files changed

+237
-323
lines changed

4 files changed

+237
-323
lines changed

src/cuda/api/detail/marshalled_options.hpp renamed to src/cuda/api/detail/option_marshalling.hpp

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_
2-
#define SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_
1+
#ifndef SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_
2+
#define SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_
33

44
#include "../types.hpp"
55

@@ -12,7 +12,7 @@
1212

1313
namespace cuda {
1414

15-
namespace rtc {
15+
namespace marshalling {
1616

1717
namespace detail_ {
1818

@@ -117,10 +117,82 @@ MarshalTarget& operator<<(MarshalTarget& mt, detail_::opt_start_t<Delimiter>& op
117117
}
118118
return mt;
119119
}
120+
121+
122+
// A partial specialization gadget
123+
template<typename CompilationOptions, typename MarshalTarget, typename Delimiter>
124+
struct gadget {
125+
/**
126+
* Uses the streaming/left-shift operator (<<) to render a delimited sequence of
127+
* command-line-argument-like options (with or without a value as relevant)
128+
* into some target entity - which could be a buffer of chars or a more complex
129+
* structure like @ref marshalled_options_t.
130+
*/
131+
static void process(
132+
const CompilationOptions &opts, MarshalTarget &marshalled, Delimiter delimiter,
133+
bool need_delimiter_after_last_option);
134+
};
135+
136+
137+
template <typename CompilationOptions, typename MarshalTarget, typename Delimiter>
138+
void process(
139+
const CompilationOptions& opts, MarshalTarget& marshalled, Delimiter delimiter,
140+
bool need_delimiter_after_last_option = false)
141+
{
142+
return detail_::gadget<CompilationOptions, MarshalTarget, Delimiter>::process(
143+
opts, marshalled, delimiter, need_delimiter_after_last_option);
144+
}
145+
120146
} // namespace detail_
121147

122-
} // namespace rtc
148+
/**
149+
* Finalize a compilation options "building" object into a structure passable to some of the
150+
* CUDA JIT compilation APIs
151+
*
152+
* @tparam Kind The kind of JITable program options to render
153+
*
154+
* @return A structure of multiple strings, passable to various CUDA APIs, but no longer
155+
* easy to modify and manipulate.
156+
*/
157+
template <typename CompilationOptions>
158+
inline detail_::marshalled_options_t marshal(const CompilationOptions& opts)
159+
{
160+
using detail_::marshalled_options_t;
161+
marshalled_options_t marshalled;
162+
// TODO: Can we easily determine the max number of options here?
163+
enum : bool { need_delimiter_after_last_option = true };
164+
marshalling::detail_::process(opts, marshalled, marshalled_options_t::advance_gadget{},
165+
need_delimiter_after_last_option);
166+
return marshalled;
167+
}
168+
169+
/**
170+
* Finalize a set of options into the form of a string appendable to a command-line
171+
*
172+
* @tparam Kind The kind of options to render
173+
*
174+
* @return a string made up of command-line options - switches and options with arguments,
175+
* designated by single or double dashes.
176+
*
177+
* @note An implementation of a processor/renderer of individual options must be
178+
* provided via detail_::process() for this function to be usable with any particular
179+
* type of options.
180+
*
181+
*/
182+
template <typename CompilationOptions>
183+
inline ::std::string render(const CompilationOptions& opts)
184+
{
185+
::std::ostringstream oss;
186+
detail_::process(opts, oss, ' ');
187+
if (oss.tellp() > 0) {
188+
// Remove the last, excessive, delimiter
189+
oss.seekp(-1,oss.cur);
190+
}
191+
return oss.str();
192+
}
193+
194+
} // namespace marshalling
123195

124196
} // namespace cuda
125197

126-
#endif /* SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_ */
198+
#endif /* SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_ */

0 commit comments

Comments
 (0)