You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry for bad English skill...
I just throw an idea.
If there's anybody like this idea, you can modify it and open an new discussion to describe your idea in better English represent.
I wish Carbon can let us write an external code in a "device lambda block".
In my example pseudocode, I used C++11 Raw String Literal style to write the device lambda block to integrating different programming/script languages.
What is a device?
Device is a framework that has its own programming/script language and runtime library(optional) but can pass in/out data by protocol(ABI) from/to Carbon both in compilation time and runtime(optional).
It means device is an one of toolchain/build-process that should be integrated into source code because it is also part of program.
The instance of device should provides compile time function and runtime time function by implement the device protocol.
"Device" is might not a good name, I just picked it.
And the device protocol should be designed as simple as possible.
Two type of devices
InternalDevice: Depends on Standard Library and Carbon runtime.
ExternalDevice: Depends on Carbon Language Device Protocol(ABI) and Runtime of Device Proxy of Implementation.
Heterogeneous Computing (Internal Device) Example in pseudocode:
// Enumerate device.// Carbon can provide some device that can be used in heterogeneous computing.
var deviceList = Carbon.ComputingDevice.enum([
ComputingDevice.DedicatedGPU, // Order is matter. first item is top priority.
ComputingDevice.IntegratedGPU,
ComputingDevice.VirtualGPU, // Emulating a GPU(and raster) by CPU with SIMD.
ComputingDevice.PhysicsCPU,
ComputingDevice.VirtualCPU
]);
for device in deviceList {
print device;
}
// Pick and use the device here.// Detail not show in here because its usage should be same as external device except the internal device can benefit of Standard Library and Syntactic Sugars.
Heterogeneous Computing by OpenGL 4.3 (External Device) Example in pseudocode:
// We do not want use Carbon's built-in device., we just use OpenGL here,
var deviceOfOpenGL = Carbon.Device.Ext.OpenGL4_3::ExternalDevice(openglContext);
// OpenGL4_3DevProxy.dll/OpenGL4_3DevProxy.so has already setup in compilation script of Carbon(like Cargo.toml in Rust).// The Type of Device Proxy - "OpenGL4_3" will be an Carbon.ExternalDevice trait compiled by Carbon compiler.// Use it to wrap openglContext as an external device instance.// So compiler know what is Carbon.Device.Ext.OpenGL4_3 and use communicate to it by device protocol.// To prepare the data that we want to pass into, all of the data that want to be used in device must be primitive data type, or any that can be converted to primitive data type.
var inArgInt32ArrA : Uint32[] = [1, 3, 5, 7, 9];
var inArgInt32ArrB : DeviceUniquePointer<Uint32> = deviceOfOpenGL.var<Uint32>([0, 2, 4, 6, 8]); // Explicit create mutable data by deviceOfOpenGL instance.
var outInt32Arr : DeviceRawPointer<Uint32> = deviceOfOpenGL.varAlloc( inArgInt32ArrA.length * sizeof(Uint32));
var futureProgram = deviceOfOpenGL.compile(
{ // Compilation Arguments.
},
[&outInt32Arr , &inArgInt32ArrA, &inArgInt32ArrB] // Capture References.R"( // Device Lambda Block Start Here!! instance of device proxy will know all of the captured reference by device protocol. { // Lambda. // The GLSL device proxy will replace some behavior on operations of "outInt32Arr", "inArgInt32ArrA" and "inArgInt32ArrB". // For example: It will insert declaration of the threes variable as Uniform Block or Shader Storage Buffer Object. int i = ...; // Calc index by thread ID, dispatch, num of threads... outInt32Arr [i] = inArgInt32ArrA[i] + // inArgInt32ArrA is not created from deviceOfOpenGL, compiler will automatically create an constant buffer here, same as deviceOfOpenGL .const([[1, 3, 5, 7, 9]]); inArgInt32ArrB[i]; })"// Device Lambda Block End Here!!
);
var compiledProgram = futureProgram.waitToGetResult();
if compiledProgram.hasErr() {
print compiledProgram.errMsg();
return;
}
var futureExecResult = compiledProgram.exec();
var result = futureExecResult.waitToGetResult();
if result.ok() {
// outInt32Arr should be [1, 5, 9, 13, 17].
}
// Release.
deviceOfOpenGL.varDealloc(outInt32Arr);
Summary:
Design a trait - "Carbon.Device" and its protocol(ABI) to provide an interface to external tools for both of compilation time and runtime.
Carbon must provide ABI to exchange arguments/data between its compiler/runtime library and external device proxy.
Anyone can implement a Device Proxy with device protocol, and setup the proxy(myProxy.dll/myProxy.so) binding in compilation script (like Cargo.toml in Rust) of Carbon, that can make Carbon be an Ecosystem Integratable Programming Language.
I did not mention any thought for safety of import device proxy because it is not the point in this idea.
Other useful cases:
C/C++ integration, static/dynamic linking and call the function of source code that hard to migrating into Carbon directly in device lambda block.
Migrating external scripts/tools used in some old C/C++ projects into Carbon source code (device lambda block) from old building system/script.
Shader Language (WGSL in WebGPU) integration.
Batch/ShellScript file integration.
Export reflection info into file (Ex: pass reflection info as arguments into external device proxy).
Inline Assembly.
Handle the atomicity manually for SIMD by ComputingDevice (atomicity of how many data width be read/written at once).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Sorry for bad English skill...
I just throw an idea.
If there's anybody like this idea, you can modify it and open an new discussion to describe your idea in better English represent.
I wish Carbon can let us write an external code in a "device lambda block".
In my example pseudocode, I used C++11 Raw String Literal style to write the device lambda block to integrating different programming/script languages.
What is a device?
Device is a framework that has its own programming/script language and runtime library(optional) but can pass in/out data by protocol(ABI) from/to Carbon both in compilation time and runtime(optional).
It means device is an one of toolchain/build-process that should be integrated into source code because it is also part of program.
The instance of device should provides compile time function and runtime time function by implement the device protocol.
"Device" is might not a good name, I just picked it.
And the device protocol should be designed as simple as possible.
Two type of devices
Heterogeneous Computing (Internal Device) Example in pseudocode:
Heterogeneous Computing by OpenGL 4.3 (External Device) Example in pseudocode:
Summary:
I did not mention any thought for safety of import device proxy because it is not the point in this idea.
Other useful cases:
Beta Was this translation helpful? Give feedback.
All reactions