1- // testArgmaxDecode.chpl
2- use ChAI; // Likely needed for core types or general setup
3- use NDArray; // Where your ndarray type and argmaxDecode method are defined
1+ use ChAI;
2+ use NDArray;
43
54proc main() {
6- // 1. Create a Chapel array with some values
7- // Use real(32) to match typical float precision in LibTorch/ChAI
85 var chapArray: [ 0 ..2 ] real (32 ) = [ 1.0 , 7.0 , 3.0 ] ; // Expected argmax index: 1
96
10- // 2. Construct an ndarray from the Chapel array.
11- // This will use your existing `proc init(const Arr: [])` in NDArray.chpl
127 var myNdArray = new ndarray(chapArray);
138
14- // 3. Call the argmaxDecode method on the ndarray instance
15- // The `this._tensorHandle` will be created/accessed via `toBridgeTensor`
16- // or the implicit cast `operator :` before calling Bridge.argmaxdecode.
179 var argmaxIndex = myNdArray.argmaxDecode();
1810
19- // 4. Print the result
2011 writeln (" Chapel array: " , chapArray);
2112 writeln (" Argmax index: " , argmaxIndex); // Should print 1 for the example above
2213
23- // Test with another example
2414 var chapArray2: [ 0 ..3 ] real (32 ) = [ 10.0 , 5.0 , 20.0 , 15.0 ] ; // Expected argmax index: 2
2515 var myNdArray2 = new ndarray(chapArray2);
2616 var argmaxIndex2 = myNdArray2.argmaxDecode();
2717 writeln (" Chapel array 2: " , chapArray2);
2818 writeln (" Argmax index 2: " , argmaxIndex2); // Should print 2
2919
30- // You can keep the accelerator checks for good measure
3120 if Bridge.acceleratorAvailable() then
3221 writeln (" Accelerator (CUDA/MPS) is available!" );
3322 else
@@ -38,8 +27,4 @@ proc main() {
3827 Bridge.debugCpuOnlyMode(false );
3928 writeln (" Debug CPU only mode is OFF. Accelerator available: " , Bridge.acceleratorAvailable());
4029
41- // IMPORTANT: Ensure tensors are freed!
42- // If `myNdArray`'s deinit() correctly calls Bridge.freeBridgeTensorHandle,
43- // then memory will be managed. If not, you might need explicit calls or a
44- // more robust memory management strategy.
4530}
0 commit comments