├── java ├── refresh_header.bat ├── DVInt32.java ├── DVInt8.java ├── DVFloat.java ├── DVInt16.java ├── DVInt64.java ├── DVDouble.java ├── DVDiscard.java ├── test │ ├── test_fill.java │ ├── test_counter.java │ ├── test_constant.java │ ├── test_for_each.java │ ├── test_discard.java │ ├── test_tabulate.java │ ├── test_find.java │ ├── test_reverse.java │ ├── test_extrema.java │ ├── test_transform_reduce.java │ ├── test_swap.java │ ├── test_inner_product.java │ ├── test_permutation.java │ ├── test_count.java │ ├── test_transform_iter.java │ ├── test_mismatch.java │ ├── test_custom_vector.java │ ├── test_sequence.java │ ├── test_transform_scan.java │ ├── test_zipped.java │ ├── test_adjacent_difference.java │ ├── test_equal.java │ ├── test_for.java │ ├── test_logical.java │ ├── test_copy.java │ ├── test_scan_by_key.java │ ├── test_trtc.java │ ├── CMakeLists.txt │ ├── test_scan.java │ └── test_replace.java ├── DVReverse.java ├── DVVectorLike.java ├── DVConstant.java ├── DVTransform.java ├── DVCounter.java ├── DVRange.java ├── DVPermutation.java ├── DVTuple.java ├── DVZipped.java ├── DVTuple.cpp ├── DVCustomVector.java ├── DeviceViewable.java ├── DVVectorAdaptor.java ├── Functor.java ├── Sorting.cpp └── For.java ├── docs ├── kmeans.png ├── _config.yml ├── histogram.png ├── DeviceViewable.png └── README.md ├── CSharp ├── ThrustRTCLR │ ├── app.ico │ ├── app.rc │ ├── Resource.h │ ├── DVTupleCLR.cpp │ ├── AssemblyInfo.cpp │ ├── Sorting.cpp │ └── FunctorCLR.cpp ├── test │ ├── test_copy │ │ ├── App.config │ │ └── Properties │ │ │ └── AssemblyInfo.cs │ ├── test_fill │ │ ├── App.config │ │ ├── test_fill.cs │ │ └── Properties │ │ │ └── AssemblyInfo.cs │ ├── test_find │ │ ├── App.config │ │ └── test_find.cs │ ├── test_for │ │ ├── App.config │ │ └── test_for.cs │ ├── test_scan │ │ └── App.config │ ├── test_sort │ │ └── App.config │ ├── test_swap │ │ ├── App.config │ │ └── test_swap.cs │ ├── test_trtc │ │ ├── App.config │ │ └── test_trtc.cs │ ├── test_constant │ │ ├── App.config │ │ └── test_constant.cs │ ├── test_count │ │ ├── App.config │ │ └── test_count.cs │ ├── test_counter │ │ ├── App.config │ │ └── test_counter.cs │ ├── test_discard │ │ ├── App.config │ │ └── test_discard.cs │ ├── test_equal │ │ ├── App.config │ │ └── test_equal.cs │ ├── test_extrema │ │ ├── App.config │ │ └── test_extrema.cs │ ├── test_for_each │ │ ├── App.config │ │ └── test_for_each.cs │ ├── test_gather │ │ └── App.config │ ├── test_logical │ │ ├── App.config │ │ └── test_logical.cs │ ├── test_merge │ │ └── App.config │ ├── test_mismatch │ │ ├── App.config │ │ └── test_mismatch.cs │ ├── test_partition │ │ └── App.config │ ├── test_reduce │ │ └── App.config │ ├── test_remove │ │ └── App.config │ ├── test_replace │ │ └── App.config │ ├── test_reverse │ │ ├── App.config │ │ └── test_reverse.cs │ ├── test_scatter │ │ └── App.config │ ├── test_sequence │ │ ├── App.config │ │ └── test_sequence.cs │ ├── test_tabulate │ │ ├── App.config │ │ └── test_tabulate.cs │ ├── test_transform │ │ └── App.config │ ├── test_unique │ │ └── App.config │ ├── test_zipped │ │ ├── App.config │ │ └── test_zipped.cs │ ├── test_binary_search │ │ └── App.config │ ├── test_custom_vector │ │ ├── App.config │ │ └── test_custom_vector.cs │ ├── test_inner_product │ │ ├── App.config │ │ └── test_inner_product.cs │ ├── test_permutation │ │ ├── App.config │ │ └── test_permutation.cs │ ├── test_scan_by_key │ │ └── App.config │ ├── test_transform_iter │ │ ├── App.config │ │ └── test_transform_iter.cs │ ├── test_transform_scan │ │ ├── App.config │ │ └── test_transform_scan.cs │ ├── test_adjacent_difference │ │ ├── App.config │ │ └── test_adjacent_difference.cs │ └── test_transform_reduce │ │ ├── App.config │ │ └── test_transform_reduce.cs └── ThrustRTCSharp │ ├── Sorting.cs │ ├── DVTuple.cs │ └── Merging.cs ├── .gitmodules ├── thirdparty └── crc64 │ └── crc64.h ├── python ├── test │ ├── test_discard.py │ ├── test_tabulate.py │ ├── test_constant.py │ ├── test_extrema.py │ ├── test_fill.py │ ├── test_range.py │ ├── test_for_each.py │ ├── test_mismatch.py │ ├── test_cupy.py │ ├── test_numba.py │ ├── test_counter.py │ ├── test_reverse.py │ ├── test_sequence.py │ ├── test_swap.py │ ├── test_transform_reduce.py │ ├── test_inner_product.py │ ├── test_fft.py │ ├── test_permutation.py │ ├── test_transform_scan.py │ ├── test_transform_iter.py │ ├── test_count.py │ ├── test_adjacent_difference.py │ ├── test_find.py │ ├── test_equal.py │ ├── test_scan_by_key.py │ ├── test_logical.py │ ├── test_multithread.py │ ├── test_for.py │ ├── test_custom_vector.py │ ├── test_scan.py │ ├── test_copy.py │ ├── test_replace.py │ ├── test_reduce.py │ ├── test_zipped.py │ ├── CMakeLists.txt │ ├── test_trtc.py │ ├── test_scatter.py │ ├── test_sort.py │ ├── test_gather.py │ └── test_remove.py ├── README.md ├── ThrustRTC │ ├── Sorting.py │ ├── DVTuple.py │ ├── Merging.py │ ├── __init__.py │ ├── utils.py │ ├── Copying.py │ └── Native.py ├── api_DVTuple.cpp ├── setup.py ├── api_Sorting.cpp ├── api_Functor.cpp ├── api_utils.cpp ├── demo │ └── histogram.py └── api_Merging.cpp ├── conda_ubuntu ├── build.sh └── meta.yaml ├── conda_windows ├── bld.bat └── meta.yaml ├── for_each.h ├── swap.h ├── fill.h ├── internal ├── launch_calc.h ├── general_find.h ├── general_scan.h ├── general_reduce.h ├── merge_sort.h ├── general_scan_by_key.h ├── general_copy_if.h ├── radix_sort.h └── Timing.h ├── tabulate.h ├── tabulate.cpp ├── for_each.cpp ├── fill.cpp ├── cpp └── test │ ├── test_for_each.cpp │ ├── test_discard.cpp │ ├── test_constant.cpp │ ├── test_transform_reduce.cpp │ ├── test_tabulate.cpp │ ├── test_counter.cpp │ ├── test_count.cpp │ ├── test_reverse.cpp │ ├── test_mismatch.cpp │ ├── test_transform_iter.cpp │ ├── test_permutation.cpp │ ├── test_swap.cpp │ ├── test_fill.cpp │ ├── test_adjacent_difference.cpp │ ├── test_equal.cpp │ ├── test_transform_scan.cpp │ ├── test_inner_product.cpp │ ├── test_for.cpp │ ├── test_sequence.cpp │ ├── test_custom_vector.cpp │ ├── test_find.cpp │ ├── test_trtc.cpp │ ├── test_logical.cpp │ ├── test_copy.cpp │ └── test_replace.cpp ├── count.h ├── fake_vectors ├── DVDiscard.h ├── DVConstant.h ├── DVCounter.h ├── DVTransform.h ├── DVDiscard.cpp ├── DVZipped.h ├── DVReverse.h ├── DVReverse.cpp ├── DVCounter.cpp ├── DVConstant.cpp ├── DVCustomVector.h ├── DVRange.h ├── DVTransform.cpp ├── DVPermutation.h ├── DVPermutation.cpp └── DVRange.cpp ├── transform_reduce.h ├── equal.h ├── DVTuple.h ├── mismatch.h ├── sequence.h ├── adjacent_difference.h ├── swap.cpp ├── logical.h ├── functor.h ├── find.h ├── TRTC_api.h ├── copy.h ├── inner_product.h ├── transform_scan.h ├── gather.h ├── scatter.h ├── transform_reduce.cpp ├── mismatch.cpp ├── replace.h ├── find.cpp ├── extrema.h ├── merge.h ├── remove.h ├── sort.h ├── transform_scan.cpp ├── partition.h ├── .github └── workflows │ ├── conda_windows.yml │ └── conda_ubuntu.yml ├── transform.h ├── DVTuple.cpp ├── count.cpp ├── reduce.h ├── adjacent_difference.cpp ├── equal.cpp ├── unique.h ├── copy.cpp ├── sequence.cpp ├── inner_product.cpp ├── scan.cpp └── logical.cpp /java/refresh_header.bat: -------------------------------------------------------------------------------- 1 | javac -h . Native.java 2 | del Native.class 3 | -------------------------------------------------------------------------------- /docs/kmeans.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fynv/ThrustRTC/HEAD/docs/kmeans.png -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker 2 | 3 | plugins: 4 | - jekyll-seo-tag 5 | -------------------------------------------------------------------------------- /docs/histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fynv/ThrustRTC/HEAD/docs/histogram.png -------------------------------------------------------------------------------- /docs/DeviceViewable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fynv/ThrustRTC/HEAD/docs/DeviceViewable.png -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/app.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fynv/ThrustRTC/HEAD/CSharp/ThrustRTCLR/app.ico -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/app.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fynv/ThrustRTC/HEAD/CSharp/ThrustRTCLR/app.rc -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "thirdparty/unqlite"] 2 | path = thirdparty/unqlite 3 | url = https://github.com/symisc/unqlite.git 4 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/Resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by app.rc 4 | -------------------------------------------------------------------------------- /thirdparty/crc64/crc64.h: -------------------------------------------------------------------------------- 1 | #ifndef _crc64_h 2 | #define _crc64_h 3 | 4 | #include 5 | 6 | uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l); 7 | 8 | #endif -------------------------------------------------------------------------------- /python/test/test_discard.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | trtc.Set_Verbose() 4 | 5 | trtc.Transform(trtc.DVCounter(trtc.DVInt32(5), 10), trtc.DVDiscard("int32_t"), trtc.Negate()) 6 | -------------------------------------------------------------------------------- /conda_ubuntu/build.sh: -------------------------------------------------------------------------------- 1 | mkdir build_python 2 | cd build_python 3 | cmake ../python -DCMAKE_BUILD_TYPE=Release 4 | make 5 | make install 6 | cd ../install/test_python 7 | $PYTHON setup.py install 8 | -------------------------------------------------------------------------------- /conda_windows/bld.bat: -------------------------------------------------------------------------------- 1 | md build_python 2 | cd build_python 3 | cmake ../python -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release 4 | nmake 5 | nmake install 6 | cd ../install/test_python 7 | "%PYTHON%" setup.py install 8 | -------------------------------------------------------------------------------- /python/test/test_tabulate.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector('int32_t', 10) 6 | 7 | trtc.Sequence(darr) 8 | trtc.Tabulate(darr, trtc.Negate()) 9 | print (darr.to_host()) 10 | -------------------------------------------------------------------------------- /CSharp/test/test_copy/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_fill/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_find/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_for/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_scan/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_sort/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_swap/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_trtc/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_constant/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_count/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_counter/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_discard/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_equal/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_extrema/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_for_each/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_gather/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_logical/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_merge/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_mismatch/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_partition/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_reduce/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_remove/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_replace/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_reverse/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_scatter/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_sequence/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_tabulate/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_transform/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_unique/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_zipped/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_binary_search/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_custom_vector/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_inner_product/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_permutation/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_scan_by_key/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_transform_iter/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_transform_scan/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | ThrustRTC 2 | ================ 3 | 4 | The purpose of the project [ThrustRTC](https://github.com/fynv/ThrustRTC) is to provide 5 | a GPU tool-set like Thrust for non-C++ launguages. This package is for Python 3. 6 | 7 | 8 | -------------------------------------------------------------------------------- /CSharp/test/test_adjacent_difference/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CSharp/test/test_transform_reduce/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /python/test/test_constant.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | darr = trtc.device_vector_from_list([3, 7, 2, 5 ], 'int32_t') 5 | trtc.Transform_Binary(darr, trtc.DVConstant(trtc.DVInt32(10)), darr, trtc.Plus()) 6 | print (darr.to_host()) 7 | -------------------------------------------------------------------------------- /python/test/test_extrema.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector_from_list([ 1, 0, 2, 2, 1, 3], 'int32_t') 6 | print (trtc.Min_Element(darr)) 7 | print (trtc.Max_Element(darr)) 8 | print (trtc.MinMax_Element(darr)) 9 | -------------------------------------------------------------------------------- /python/test/test_fill.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | darr = trtc.device_vector('int32_t', 5) 5 | trtc.Fill(darr, trtc.DVInt32(123)) 6 | print (darr.to_host()) 7 | 8 | trtc.Fill(darr.range(1,3), trtc.DVInt32(456)) 9 | print (darr.to_host()) 10 | 11 | -------------------------------------------------------------------------------- /python/test/test_range.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | dIn = trtc.device_vector_from_list([ 10, 20, 30, 40, 50, 60, 70, 80 ], 'int32_t') 3 | dRange = dIn.range(2, 6) 4 | dOut = trtc.device_vector('int32_t', 4) 5 | 6 | trtc.Copy(dRange, dOut) 7 | print (dOut.to_host()) 8 | -------------------------------------------------------------------------------- /for_each.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_for_each_h 2 | #define _TRTC_for_each_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DVVector.h" 7 | #include "functor.h" 8 | 9 | bool THRUST_RTC_API TRTC_For_Each(DVVectorLike& vec, const Functor& f); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /java/DVInt32.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVInt32 extends DeviceViewable 4 | { 5 | public DVInt32(int v) 6 | { 7 | super(Native.dvint32_create(v)); 8 | } 9 | 10 | public int value() 11 | { 12 | return Native.dvint32_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/DVInt8.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVInt8 extends DeviceViewable 4 | { 5 | public DVInt8(byte v) 6 | { 7 | super(Native.dvint8_create(v)); 8 | } 9 | 10 | public byte value() 11 | { 12 | return Native.dvint8_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /swap.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_swap_h 2 | #define _TRTC_swap_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | 9 | bool THRUST_RTC_API TRTC_Swap(DVVectorLike& vec1, DVVectorLike& vec2); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /java/DVFloat.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVFloat extends DeviceViewable 4 | { 5 | public DVFloat(float v) 6 | { 7 | super(Native.dvfloat_create(v)); 8 | } 9 | 10 | public float value() 11 | { 12 | return Native.dvfloat_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/DVInt16.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVInt16 extends DeviceViewable 4 | { 5 | public DVInt16(short v) 6 | { 7 | super(Native.dvint16_create(v)); 8 | } 9 | 10 | public short value() 11 | { 12 | return Native.dvint16_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/DVInt64.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVInt64 extends DeviceViewable 4 | { 5 | public DVInt64(long v) 6 | { 7 | super(Native.dvint64_create(v)); 8 | } 9 | 10 | public long value() 11 | { 12 | return Native.dvint64_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /fill.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_fill_h 2 | #define _TRTC_fill_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | 9 | bool THRUST_RTC_API TRTC_Fill(DVVectorLike& vec, const DeviceViewable& value); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /java/DVDouble.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVDouble extends DeviceViewable 4 | { 5 | public DVDouble(double v) 6 | { 7 | super(Native.dvdouble_create(v)); 8 | } 9 | 10 | public double value() 11 | { 12 | return Native.dvdouble_value(cptr()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /python/test/test_for_each.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | printf_functor = trtc.Functor( {}, ['x'], 6 | ''' 7 | printf("%d\\n", x); 8 | ''') 9 | 10 | darr = trtc.device_vector_from_list([1,2,3,1,2], 'int32_t') 11 | trtc.For_Each(darr, printf_functor) 12 | 13 | -------------------------------------------------------------------------------- /python/test/test_mismatch.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | d1 = trtc.device_vector_from_list([0, 5, 3, 7], 'int32_t') 6 | d2 = trtc.device_vector_from_list([0, 5, 8, 7], 'int32_t') 7 | 8 | print(trtc.Mismatch(d1, d2)) 9 | print(trtc.Mismatch(d1, d2, trtc.EqualTo())) 10 | -------------------------------------------------------------------------------- /internal/launch_calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _launch_calc_h 2 | #define _launch_calc_h 3 | 4 | #include "cuda_wrapper.h" 5 | bool launch_calc(CUfunction func, unsigned sharedMemBytes, int& sizeBlock); 6 | bool persist_calc(CUfunction func, unsigned sharedMemBytes, int sizeBlock, int& numBlocks); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /python/test/test_cupy.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | import numpy as np 3 | import cupy as cp 4 | 5 | nparr = np.array([1, 0, 2, 2, 1, 3], dtype=np.int32) 6 | cparr = cp.array(nparr) 7 | darr = trtc.DVCupyVector(cparr) 8 | trtc.Inclusive_Scan(darr, darr) 9 | print(cp.asnumpy(cparr)) 10 | -------------------------------------------------------------------------------- /python/test/test_numba.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | import numpy as np 3 | from numba import cuda 4 | 5 | nparr = np.array([1, 0, 2, 2, 1, 3], dtype=np.int32) 6 | nbarr = cuda.to_device(nparr) 7 | darr = trtc.DVNumbaVector(nbarr) 8 | trtc.Inclusive_Scan(darr, darr) 9 | print(nbarr.copy_to_host()) 10 | -------------------------------------------------------------------------------- /python/test/test_counter.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | negate = trtc.Functor( {}, ['x'], 6 | ''' 7 | return -x; 8 | ''') 9 | 10 | 11 | darr = trtc.device_vector('int32_t', 10) 12 | trtc.Transform(trtc.DVCounter(trtc.DVInt32(5), 10), darr, trtc.Negate()) 13 | print (darr.to_host()) 14 | -------------------------------------------------------------------------------- /java/DVDiscard.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVDiscard extends DVVectorLike 4 | { 5 | public DVDiscard(String elem_cls, int size) 6 | { 7 | super(Native.dvdiscard_create(elem_cls, size)); 8 | } 9 | 10 | public DVDiscard(String elem_cls) 11 | { 12 | this(elem_cls, -1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tabulate.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_tabulate_h 2 | #define _TRTC_tabulate_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Tabulate(DVVectorLike& vec, const Functor& op); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /internal/general_find.h: -------------------------------------------------------------------------------- 1 | #ifndef _general_find_h 2 | #define _general_find_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | 11 | bool general_find(size_t n, const Functor src, size_t& result); 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /python/test/test_reverse.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | dinput = trtc.device_vector_from_list([3, 7, 2, 5], 'int32_t') 6 | doutput = trtc.device_vector('int32_t', 4) 7 | 8 | dreverse = trtc.DVReverse(dinput) 9 | 10 | trtc.Transform(dreverse, doutput, trtc.Negate()) 11 | print (doutput.to_host()) 12 | 13 | -------------------------------------------------------------------------------- /tabulate.cpp: -------------------------------------------------------------------------------- 1 | #include "tabulate.h" 2 | 3 | bool TRTC_Tabulate(DVVectorLike& vec, const Functor& op) 4 | { 5 | static TRTC_For s_for({ "view_vec", "op" }, "idx", 6 | " view_vec[idx] = op(view_vec[idx]);\n" 7 | ); 8 | 9 | const DeviceViewable* args[] = { &vec, &op }; 10 | return s_for.launch_n(vec.size(), args); 11 | } 12 | -------------------------------------------------------------------------------- /python/test/test_sequence.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector('int32_t', 10) 6 | 7 | trtc.Sequence(darr) 8 | print (darr.to_host()) 9 | 10 | trtc.Sequence(darr, trtc.DVInt32(1)) 11 | print (darr.to_host()) 12 | 13 | trtc.Sequence(darr, trtc.DVInt32(1), trtc.DVInt32(3)) 14 | print (darr.to_host()) 15 | -------------------------------------------------------------------------------- /for_each.cpp: -------------------------------------------------------------------------------- 1 | #include "for_each.h" 2 | #include "DeviceViewable.h" 3 | 4 | bool TRTC_For_Each(DVVectorLike& vec, const Functor& f) 5 | { 6 | static TRTC_For s_for({ "view_vec", "f"}, "idx", 7 | " f(view_vec[idx]);\n" 8 | ); 9 | 10 | const DeviceViewable* args[] = { &vec, &f }; 11 | return s_for.launch_n(vec.size(), args); 12 | } 13 | -------------------------------------------------------------------------------- /internal/general_scan.h: -------------------------------------------------------------------------------- 1 | #ifndef _general_scan_h 2 | #define _general_scan_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool general_scan(size_t n, const Functor& src, DVVectorLike& vec_out, const Functor& binary_op); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /python/test/test_swap.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr1 = trtc.device_vector_from_list([ 10, 20, 30, 40, 50, 60, 70, 80], 'int32_t') 6 | darr2 = trtc.device_vector_from_list([ 1000, 900, 800, 700, 600, 500, 400, 300 ], 'int32_t') 7 | 8 | trtc.Swap(darr1, darr2) 9 | print (darr1.to_host()) 10 | print (darr2.to_host()) 11 | -------------------------------------------------------------------------------- /fill.cpp: -------------------------------------------------------------------------------- 1 | #include "fill.h" 2 | 3 | bool TRTC_Fill(DVVectorLike& vec, const DeviceViewable& value) 4 | { 5 | static TRTC_For s_for( 6 | { "view_vec", "value" }, "idx", 7 | " view_vec[idx]=(decltype(view_vec)::value_t)value;" 8 | ); 9 | 10 | const DeviceViewable* args[] = { &vec, &value }; 11 | return s_for.launch_n(vec.size(), args); 12 | } 13 | -------------------------------------------------------------------------------- /java/test/test_fill.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_fill 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector vec_to_fill = new DVVector("int32_t", 5); 9 | TRTC.Fill(vec_to_fill, new DVInt32(123)); 10 | System.out.println(Arrays.toString((int[])vec_to_fill.to_host())); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /python/test/test_transform_reduce.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector_from_list([ -1, 0, -2, -2, 1, -3], 'int32_t') 6 | 7 | absolute_value = trtc.Functor( {}, ['x'], 8 | ''' 9 | return x<(decltype(x))0 ? -x : x; 10 | ''') 11 | 12 | print(trtc.Transform_Reduce(darr, absolute_value, trtc.DVInt32(0), trtc.Maximum())) 13 | -------------------------------------------------------------------------------- /internal/general_reduce.h: -------------------------------------------------------------------------------- 1 | #ifndef _general_reduce_h 2 | #define _general_reduce_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool general_reduce(size_t n, const char* name_cls, const Functor& src, const Functor& op, ViewBuf& ret_buf); 11 | 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /python/test/test_inner_product.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr1 = trtc.device_vector_from_list([ 1.0, 2.0, 5.0], 'float') 6 | darr2 = trtc.device_vector_from_list([ 4.0, 1.0, 5.0], 'float') 7 | print (trtc.Inner_Product(darr1, darr2, trtc.DVFloat(0.0))) 8 | print (trtc.Inner_Product(darr1, darr2, trtc.DVFloat(0.0), trtc.Plus(), trtc.Multiplies())) 9 | 10 | -------------------------------------------------------------------------------- /cpp/test/test_for_each.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "for_each.h" 5 | 6 | int main() 7 | { 8 | Functor printf_functor = {{}, { "x" }, " printf(\"%d\\n\", x);\n" }; 9 | 10 | int hvec[5] = { 1, 2, 3, 1, 2 }; 11 | DVVector vec("int32_t", 5, hvec); 12 | TRTC_For_Each(vec, printf_functor); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /java/test/test_counter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_counter 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVCounter src = new DVCounter(new DVInt32(1), 10); 9 | DVVector dst = new DVVector("int32_t", 10); 10 | TRTC.Copy(src, dst); 11 | System.out.println(Arrays.toString((int[])dst.to_host())); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /java/test/test_constant.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_constant 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVConstant src = new DVConstant(new DVInt32(123), 10); 9 | DVVector dst = new DVVector("int32_t", 10); 10 | TRTC.Copy(src, dst); 11 | System.out.println(Arrays.toString((int[])dst.to_host())); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /java/test/test_for_each.java: -------------------------------------------------------------------------------- 1 | import JThrustRTC.*; 2 | 3 | public class test_for_each 4 | { 5 | public static void main(String[] args) 6 | { 7 | Functor printf_functor = new Functor(new String[]{ "x" }, " printf(\"%d\\n\", x);\n"); 8 | DVVector vec = new DVVector(new int[] { 1, 2, 3, 1, 2 }); 9 | TRTC.For_Each(vec, printf_functor); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /internal/merge_sort.h: -------------------------------------------------------------------------------- 1 | #ifndef _merge_sort_h 2 | #define _merge_sort_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool merge_sort(DVVectorLike& vec, const Functor& comp); 11 | bool merge_sort_by_key(DVVectorLike& keys, DVVectorLike& values, const Functor& comp); 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /java/test/test_discard.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_discard 5 | { 6 | public static void main(String[] args) 7 | { 8 | TRTC.Set_Verbose(); 9 | 10 | // just to verify that it compiles 11 | DVCounter src = new DVCounter(new DVInt32(5), 10); 12 | DVDiscard sink = new DVDiscard("int32_t"); 13 | TRTC.Copy(src, sink); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /python/test/test_fft.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cupy as cp 3 | import ThrustRTC as trtc 4 | 5 | cparr = cp.empty(4, dtype = np.complex64) 6 | darr = trtc.DVCupyVector(cparr) 7 | c = complex(1.0, 2.0) 8 | trtc.Fill(darr, trtc.DVComplex64(c)) 9 | print("input: ", cp.asnumpy(cparr)) 10 | cparr = cp.fft.fft(cparr) 11 | # darr = trtc.DVCupyVector(cparr) 12 | print("output: ", cp.asnumpy(cparr)) 13 | -------------------------------------------------------------------------------- /java/test/test_tabulate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_tabulate 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector vec = new DVVector("int32_t", 10); 9 | TRTC.Sequence(vec); 10 | TRTC.Tabulate(vec, new Functor("Negate")); 11 | System.out.println(Arrays.toString((int[])vec.to_host())); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /cpp/test/test_discard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "fake_vectors/DVCounter.h" 4 | #include "fake_vectors/DVDiscard.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | TRTC_Set_Verbose(); 10 | 11 | // just to verify that it compiles 12 | DVDiscard sink("int32_t"); 13 | TRTC_Transform(DVCounter(DVInt32(5), 10), sink, Functor("Negate")); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /count.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_count_h 2 | #define _TRTC_count_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Count(const DVVectorLike& vec, const DeviceViewable& value, size_t& ret); 11 | bool THRUST_RTC_API TRTC_Count_If(const DVVectorLike& vec, const Functor& pred, size_t& ret); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /fake_vectors/DVDiscard.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVDiscard_h 2 | #define _DVDiscard_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVDiscard : public DVVectorLike 7 | { 8 | public: 9 | DVDiscard(const char* elem_cls, size_t size = (size_t)(-1)); 10 | virtual ViewBuf view() const; 11 | virtual bool is_readable() const { return false; } 12 | virtual bool is_writable() const { return true; } 13 | 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /java/test/test_find.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_find 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector d_values = new DVVector(new int[] { 0, 5, 3, 7 }); 9 | System.out.println(TRTC.Find(d_values, new DVInt32(3))); 10 | System.out.println(TRTC.Find(d_values, new DVInt32(5))); 11 | System.out.println(TRTC.Find(d_values, new DVInt32(9))); 12 | } 13 | } -------------------------------------------------------------------------------- /transform_reduce.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_transform_reduce_h 2 | #define _TRTC_transform_reduce_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Transform_Reduce(const DVVectorLike& vec, const Functor& unary_op, const DeviceViewable& init, const Functor& binary_op, ViewBuf& ret); 11 | 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /java/DVReverse.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVReverse extends DVVectorLike 4 | { 5 | public DVReverse(DVVectorLike vec_value) 6 | { 7 | super(Native.dvreverse_create(vec_value.cptr())); 8 | m_vec_value = vec_value; 9 | } 10 | 11 | private DVVectorLike m_vec_value; 12 | 13 | @Override 14 | public void close() 15 | { 16 | m_vec_value = null; 17 | super.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /python/test/test_permutation.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | dvalues = trtc.device_vector_from_list([10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0], 'float') 6 | dindices = trtc.device_vector_from_list([2,6,1,3], 'int32_t') 7 | doutput = trtc.device_vector('float', 4) 8 | 9 | perm = trtc.DVPermutation(dvalues, dindices) 10 | 11 | trtc.Transform(perm, doutput, trtc.Negate()) 12 | print (doutput.to_host()) 13 | 14 | -------------------------------------------------------------------------------- /fake_vectors/DVConstant.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVConstant_h 2 | #define _DVConstant_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVConstant : public DVVectorLike 7 | { 8 | public: 9 | ViewBuf value() const { return m_value; } 10 | DVConstant(const DeviceViewable& dvobj, size_t size = (size_t)(-1)); 11 | virtual ViewBuf view() const; 12 | 13 | private: 14 | ViewBuf m_value; 15 | size_t m_offsets[3]; 16 | }; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /internal/general_scan_by_key.h: -------------------------------------------------------------------------------- 1 | #ifndef _general_scan_by_key_h 2 | #define _general_scan_by_key_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool general_scan_by_key(size_t n, const Functor& value_in, const DVVectorLike& key, DVVectorLike& value_out, const Functor& binary_pred, const Functor& binary_op); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /python/ThrustRTC/Sorting.py: -------------------------------------------------------------------------------- 1 | from .Native import ffi, native, check_i 2 | 3 | def Sort(vec, comp = None): 4 | cptr_comp = ffi.NULL 5 | if comp!=None: 6 | cptr_comp = comp.m_cptr 7 | check_i(native.n_sort(vec.m_cptr, cptr_comp)) 8 | 9 | def Sort_By_Key(keys, values, comp = None): 10 | cptr_comp = ffi.NULL 11 | if comp!=None: 12 | cptr_comp = comp.m_cptr 13 | check_i(native.n_sort_by_key(keys.m_cptr, values.m_cptr, cptr_comp)) 14 | -------------------------------------------------------------------------------- /java/test/test_reverse.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_reverse 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector dvalues = new DVVector(new int[] { 3, 7, 2, 5 }); 9 | DVReverse src = new DVReverse(dvalues); 10 | DVVector dst = new DVVector("int32_t", 4); 11 | TRTC.Copy(src, dst); 12 | System.out.println(Arrays.toString((int[])dst.to_host())); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /equal.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_equal_h 2 | #define _TRTC_equal_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Equal(const DVVectorLike& vec1, const DVVectorLike& vec2, bool& ret); 11 | bool THRUST_RTC_API TRTC_Equal(const DVVectorLike& vec1, const DVVectorLike& vec2, const Functor& binary_pred, bool& ret); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /python/test/test_transform_scan.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 6 | trtc.Transform_Inclusive_Scan(darr, darr, trtc.Negate(), trtc.Plus()) 7 | print (darr.to_host()) 8 | 9 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 10 | trtc.Transform_Exclusive_Scan(darr, darr, trtc.Negate(), trtc.DVInt32(4), trtc.Plus()) 11 | print (darr.to_host()) 12 | 13 | -------------------------------------------------------------------------------- /fake_vectors/DVCounter.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVCounter_h 2 | #define _DVCounter_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVCounter : public DVVectorLike 7 | { 8 | public: 9 | ViewBuf value_init() const { return m_value_init; } 10 | DVCounter(const DeviceViewable& dvobj_init, size_t size = (size_t)(-1)); 11 | virtual ViewBuf view() const; 12 | 13 | private: 14 | ViewBuf m_value_init; 15 | size_t m_offsets[3]; 16 | }; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /java/test/test_extrema.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_extrema 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector d_data = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 9 | System.out.println(TRTC.Min_Element(d_data)); 10 | System.out.println(TRTC.Max_Element(d_data)); 11 | System.out.println(Arrays.toString(TRTC.MinMax_Element(d_data))); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DVTuple.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVTuple_h 2 | #define _DVTuple_h 3 | 4 | #include "TRTC_api.h" 5 | #include "DeviceViewable.h" 6 | #include "TRTCContext.h" 7 | 8 | class THRUST_RTC_API DVTuple : public DeviceViewable 9 | { 10 | public: 11 | DVTuple(const std::vector& elem_map); 12 | virtual ViewBuf view() const; 13 | 14 | private: 15 | std::vector m_view_elems; 16 | std::vector m_offsets; 17 | 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /python/test/test_transform_iter.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | square_root = trtc.Functor( {}, ['x'], 6 | ''' 7 | return sqrtf(x); 8 | ''') 9 | 10 | dvalues = trtc.device_vector_from_list([1.0, 4.0, 9.0, 16.0], 'float') 11 | doutput = trtc.device_vector('float', 4) 12 | 13 | dtrans = trtc.DVTransform(dvalues, 'float', square_root) 14 | 15 | trtc.Transform(dtrans, doutput, trtc.Negate()) 16 | print (doutput.to_host()) 17 | 18 | -------------------------------------------------------------------------------- /fake_vectors/DVTransform.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVTransform_h 2 | #define _DVTransform_h 3 | 4 | #include "DVVector.h" 5 | #include "functor.h" 6 | 7 | class THRUST_RTC_API DVTransform : public DVVectorLike 8 | { 9 | public: 10 | DVTransform(const DVVectorLike& vec_in, const char* elem_cls, const Functor& op); 11 | virtual ViewBuf view() const; 12 | 13 | private: 14 | ViewBuf m_view_in; 15 | ViewBuf m_view_op; 16 | size_t m_offsets[3]; 17 | }; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /CSharp/test/test_for_each/test_for_each.cs: -------------------------------------------------------------------------------- 1 | using ThrustRTCSharp; 2 | 3 | namespace test_for_each 4 | { 5 | class test_for_each 6 | { 7 | static void Main(string[] args) 8 | { 9 | Functor printf_functor = new Functor(new string[]{ "x" }, " printf(\"%d\\n\", x);\n"); 10 | DVVector vec = new DVVector(new int[] { 1, 2, 3, 1, 2 }); 11 | TRTC.For_Each(vec, printf_functor); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /fake_vectors/DVDiscard.cpp: -------------------------------------------------------------------------------- 1 | #include "DVDiscard.h" 2 | 3 | DVDiscard::DVDiscard(const char* elem_cls, size_t size) 4 | :DVVectorLike(elem_cls, (std::string("_Sink<")+ elem_cls +">&").c_str(), size) 5 | { 6 | m_name_view_cls = std::string("DiscardView<") + m_elem_cls + ">"; 7 | } 8 | 9 | ViewBuf DVDiscard::view() const 10 | { 11 | ViewBuf buf(sizeof(size_t) + 1); 12 | size_t& size = *(size_t*)(buf.data()); 13 | size = m_size; 14 | return buf; 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /mismatch.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_mismatch_h 2 | #define _TRTC_mismatch_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Mismatch(const DVVectorLike& vec1, const DVVectorLike& vec2, size_t& result); 11 | bool THRUST_RTC_API TRTC_Mismatch(const DVVectorLike& vec1, const DVVectorLike& vec2, const Functor& pred, size_t& result); 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /sequence.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_sequence_h 2 | #define _TRTC_sequence_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | 9 | bool THRUST_RTC_API TRTC_Sequence(DVVectorLike& vec); 10 | bool THRUST_RTC_API TRTC_Sequence(DVVectorLike& vec, const DeviceViewable& value_init); 11 | bool THRUST_RTC_API TRTC_Sequence(DVVectorLike& vec, const DeviceViewable& value_init, const DeviceViewable& value_step); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /python/test/test_count.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | op = trtc.Functor( {}, ['x'], 6 | ''' 7 | return x % 100; 8 | ''') 9 | 10 | 11 | darr = trtc.device_vector('int32_t', 2000) 12 | trtc.Transform(trtc.DVCounter(trtc.DVInt32(0), 2000), darr, op) 13 | print(trtc.Count(darr, trtc.DVInt32(47))) 14 | 15 | 16 | op2 = trtc.Functor({}, ['x'], 17 | ''' 18 | return (x % 100)==47; 19 | ''') 20 | 21 | trtc.Sequence(darr) 22 | print(trtc.Count_If(darr, op2)) 23 | -------------------------------------------------------------------------------- /CSharp/test/test_discard/test_discard.cs: -------------------------------------------------------------------------------- 1 | using ThrustRTCSharp; 2 | 3 | namespace test_discard 4 | { 5 | class test_discard 6 | { 7 | static void Main(string[] args) 8 | { 9 | TRTC.Set_Verbose(); 10 | 11 | // just to verify that it compiles 12 | DVCounter src = new DVCounter(new DVInt32(5), 10); 13 | DVDiscard sink = new DVDiscard("int32_t"); 14 | TRTC.Copy(src, sink); 15 | 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /adjacent_difference.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_adjacent_difference_h 2 | #define _TRTC_adjacent_difference_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Adjacent_Difference(const DVVectorLike& vec_in, DVVectorLike& vec_out); 11 | bool THRUST_RTC_API TRTC_Adjacent_Difference(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& binary_op); 12 | 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /cpp/test/test_constant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVConstant.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | int hvalues[4] = { 3, 7, 2, 5 }; 10 | DVVector vec("int32_t", 4, hvalues); 11 | 12 | TRTC_Transform_Binary(vec, DVConstant(DVInt32(10)), vec, Functor("Plus")); 13 | vec.to_host(hvalues); 14 | printf("%d %d %d %d\n", hvalues[0], hvalues[1], hvalues[2], hvalues[3]); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /swap.cpp: -------------------------------------------------------------------------------- 1 | #include "swap.h" 2 | 3 | bool TRTC_Swap(DVVectorLike& vec1, DVVectorLike& vec2) 4 | { 5 | static TRTC_For s_for( 6 | { "view_vec1", "view_vec2" }, "idx", 7 | " decltype(view_vec1)::value_t t = view_vec1[idx];\n" 8 | " view_vec1[idx] = (decltype(view_vec1)::value_t)view_vec2[idx];\n " 9 | " view_vec2[idx]=(decltype(view_vec2)::value_t)t;\n" 10 | ); 11 | 12 | const DeviceViewable* args[] = { &vec1, &vec2 }; 13 | return s_for.launch_n( vec1.size(), args); 14 | } 15 | -------------------------------------------------------------------------------- /logical.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_logical_h 2 | #define _TRTC_logical_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_All_Of(const DVVectorLike& vec, const Functor& pred, bool& ret); 11 | bool THRUST_RTC_API TRTC_Any_Of(const DVVectorLike& vec, const Functor& pred, bool& ret); 12 | bool THRUST_RTC_API TRTC_None_Of(const DVVectorLike& vec, const Functor& pred, bool& ret); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /CSharp/test/test_extrema/test_extrema.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_extrema 5 | { 6 | class test_extrema 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector d_data = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 11 | Console.WriteLine(TRTC.Min_Element(d_data)); 12 | Console.WriteLine(TRTC.Max_Element(d_data)); 13 | Console.WriteLine(TRTC.MinMax_Element(d_data)); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/test/test_transform_reduce.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_transform_reduce 5 | { 6 | public static void main(String[] args) 7 | { 8 | Functor absolute_value = new Functor ( new String[]{ "x" }, " return x<(decltype(x))0 ? -x : x;\n" ); 9 | DVVector d_data = new DVVector(new int[] { -1, 0, -2, -2, 1, -3 }); 10 | System.out.println(TRTC.Transform_Reduce(d_data, absolute_value, new DVInt32(0), new Functor("Maximum"))); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /functor.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_Functor_h 2 | #define _TRTC_Functor_h 3 | 4 | #include "TRTCContext.h" 5 | 6 | class THRUST_RTC_API Functor : public DeviceViewable 7 | { 8 | public: 9 | Functor(const std::vector& arg_map, const std::vector& functor_params, const char* code_body); 10 | Functor(const char* name_built_in_view_cls); 11 | 12 | virtual ViewBuf view() const; 13 | 14 | private: 15 | std::vector m_view_args; 16 | std::vector m_offsets; 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /java/DVVectorLike.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVVectorLike extends DeviceViewable 4 | { 5 | public DVVectorLike(long _cptr) 6 | { 7 | super(_cptr); 8 | } 9 | 10 | public String name_elem_cls() 11 | { 12 | return Native.dvvectorlike_name_elem_cls(cptr()); 13 | } 14 | 15 | public int size() 16 | { 17 | return Native.dvvectorlike_size(cptr()); 18 | } 19 | 20 | public DVRange range(int begin, int end) 21 | { 22 | return new DVRange(this, begin, end); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /java/test/test_swap.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_swap 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector darr1 = new DVVector(new int[] { 10, 20, 30, 40, 50, 60, 70, 80 }); 9 | DVVector darr2 = new DVVector(new int[] { 1000, 900, 800, 700, 600, 500, 400, 300 }); 10 | TRTC.Swap(darr1, darr2); 11 | System.out.println(Arrays.toString((int[])darr1.to_host())); 12 | System.out.println(Arrays.toString((int[])darr2.to_host())); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CSharp/test/test_find/test_find.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_find 5 | { 6 | class test_find 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector d_values = new DVVector(new int[] { 0, 5, 3, 7 }); 11 | Console.WriteLine(TRTC.Find(d_values, new DVInt32(3))); 12 | Console.WriteLine(TRTC.Find(d_values, new DVInt32(5))); 13 | Console.WriteLine(TRTC.Find(d_values, new DVInt32(9))); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /find.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_find_h 2 | #define _TRTC_find_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Find(const DVVectorLike& vec, const DeviceViewable& value, size_t& result); 11 | bool THRUST_RTC_API TRTC_Find_If(const DVVectorLike& vec, const Functor& pred, size_t& result); 12 | bool THRUST_RTC_API TRTC_Find_If_Not(const DVVectorLike& vec, const Functor& pred, size_t& result); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /java/DVConstant.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVConstant extends DVVectorLike 4 | { 5 | public DVConstant(DeviceViewable dvobj, int size) 6 | { 7 | super(Native.dvconstant_create(dvobj.cptr(), size)); 8 | m_dvobj = dvobj; 9 | } 10 | 11 | public DVConstant(DeviceViewable dvobj) 12 | { 13 | this(dvobj, -1); 14 | } 15 | 16 | private DeviceViewable m_dvobj; 17 | 18 | @Override 19 | public void close() 20 | { 21 | m_dvobj = null; 22 | super.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /python/test/test_adjacent_difference.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr1_in = trtc.device_vector_from_list([ 1, 2, 1, 2, 1, 2, 1, 2], 'int32_t') 6 | darr1_out = trtc.device_vector('int32_t', 8) 7 | trtc.Adjacent_Difference(darr1_in, darr1_out) 8 | print (darr1_out.to_host()) 9 | 10 | darr2_in = trtc.device_vector_from_list([ 1, 2, 1, 2, 1, 2, 1, 2], 'int32_t') 11 | darr2_out = trtc.device_vector('int32_t', 8) 12 | 13 | trtc.Adjacent_Difference(darr2_in, darr2_out, trtc.Plus()) 14 | print (darr2_out.to_host()) 15 | 16 | -------------------------------------------------------------------------------- /cpp/test/test_transform_reduce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "transform_reduce.h" 5 | 6 | int main() 7 | { 8 | Functor absolute_value = { {},{ "x" }, " return x<(decltype(x))0 ? -x : x;\n" }; 9 | 10 | int h_data[6] = { -1, 0, -2, -2, 1, -3 }; 11 | DVVector d_data("int32_t", 6, h_data); 12 | 13 | ViewBuf res; 14 | TRTC_Transform_Reduce(d_data, absolute_value, DVInt32(0), Functor("Maximum"), res); 15 | printf("%d\n", *(int*)res.data()); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /java/DVTransform.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVTransform extends DVVectorLike 4 | { 5 | public DVTransform(DVVectorLike vec_in, String elem_cls, Functor op) 6 | { 7 | super(Native.dvtransform_create(vec_in.cptr(), elem_cls, op.cptr())); 8 | m_vec_in = vec_in; 9 | m_op = op; 10 | } 11 | 12 | private DVVectorLike m_vec_in; 13 | private Functor m_op; 14 | 15 | @Override 16 | public void close() 17 | { 18 | m_vec_in = null; 19 | m_op = null; 20 | super.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /conda_ubuntu/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: thrustrtc 3 | version: 0.3.20 4 | 5 | source: 6 | path: .. 7 | 8 | requirements: 9 | build: 10 | - python 11 | - setuptools 12 | - cffi 13 | - numpy 14 | 15 | run: 16 | - python 17 | - cffi 18 | - numpy 19 | 20 | test: 21 | imports: 22 | - ThrustRTC 23 | 24 | about: 25 | home: https://github.com/fynv/ThrustRTC 26 | license: Anti-996 27 | summary: CUDA tool set for Python that provides similar functionality like Thrust, with NVRTC at its core. 28 | -------------------------------------------------------------------------------- /conda_windows/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: thrustrtc 3 | version: 0.3.20 4 | 5 | source: 6 | path: .. 7 | 8 | requirements: 9 | build: 10 | - python 11 | - setuptools 12 | - cffi 13 | - numpy 14 | 15 | run: 16 | - python 17 | - cffi 18 | - numpy 19 | 20 | test: 21 | imports: 22 | - ThrustRTC 23 | 24 | about: 25 | home: https://github.com/fynv/ThrustRTC 26 | license: Anti-996 27 | summary: CUDA tool set for Python that provides similar functionality like Thrust, with NVRTC at its core. 28 | -------------------------------------------------------------------------------- /TRTC_api.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_API_h 2 | #define _TRTC_API_h 3 | 4 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) 5 | #pragma warning( disable: 4275 ) 6 | #pragma warning( disable: 4251 ) 7 | #pragma warning( disable: 4530 ) 8 | #if defined THRUST_RTC_DLL_EXPORT 9 | #define THRUST_RTC_API __declspec(dllexport) 10 | #elif defined THRUST_RTC_DLL_IMPORT 11 | #define THRUST_RTC_API __declspec(dllimport) 12 | #endif 13 | #endif 14 | 15 | #ifndef THRUST_RTC_API 16 | #define THRUST_RTC_API 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /cpp/test/test_tabulate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "sequence.h" 5 | #include "tabulate.h" 6 | 7 | int main() 8 | { 9 | int hvalues[10]; 10 | DVVector vec("int32_t", 10); 11 | TRTC_Sequence(vec); 12 | TRTC_Tabulate(vec, Functor("Negate")); 13 | vec.to_host(hvalues); 14 | printf("%d %d %d %d %d ", hvalues[0], hvalues[1], hvalues[2], hvalues[3], hvalues[4]); 15 | printf("%d %d %d %d %d\n", hvalues[5], hvalues[6], hvalues[7], hvalues[8], hvalues[9]); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /internal/general_copy_if.h: -------------------------------------------------------------------------------- 1 | #ifndef _general_copy_if_h 2 | #define _general_copy_if_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | uint32_t general_copy_if(size_t n, const Functor& src_scan, const DVVectorLike& vec_in, DVVectorLike& vec_out); 11 | uint32_t general_copy_if(size_t n, const Functor& src_scan, const DVVectorLike& vec_in1, const DVVectorLike& vec_in2, DVVectorLike& vec_out1, DVVectorLike& vec_out2); 12 | 13 | 14 | #endif 15 | 16 | -------------------------------------------------------------------------------- /java/DVCounter.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVCounter extends DVVectorLike 4 | { 5 | public DVCounter(DeviceViewable dvobj_init, int size) 6 | { 7 | super(Native.dvcounter_create(dvobj_init.cptr(), size)); 8 | m_dvobj_init = dvobj_init; 9 | } 10 | 11 | public DVCounter(DeviceViewable dvobj) 12 | { 13 | this(dvobj, -1); 14 | } 15 | 16 | private DeviceViewable m_dvobj_init; 17 | 18 | @Override 19 | public void close() 20 | { 21 | m_dvobj_init = null; 22 | super.close(); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # ThrustRTC 2 | 3 | ## Quick Start Guide 4 | 5 | [Quick Start Guide - for Python users](https://fynv.github.io/ThrustRTC/QuickStartGuide.html) 6 | 7 | [Quick Start Guide - for C# users](https://fynv.github.io/ThrustRTC/QuickStartGuide_cs.html) 8 | 9 | [Quick Start Guide - for JAVA users](https://fynv.github.io/ThrustRTC/QuickStartGuide_java.html) 10 | 11 | ## Demos 12 | 13 | Using ThrustRTC for histogram calculation and k-means clustering. 14 | 15 | [https://fynv.github.io/ThrustRTC/Demo.html](https://fynv.github.io/ThrustRTC/Demo.html) 16 | -------------------------------------------------------------------------------- /cpp/test/test_counter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVCounter.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | int hvalues[10]; 10 | DVVector vec("int32_t", 10); 11 | TRTC_Transform(DVCounter(DVInt32(5), 10), vec, Functor("Negate")); 12 | vec.to_host(hvalues); 13 | printf("%d %d %d %d %d ", hvalues[0], hvalues[1], hvalues[2], hvalues[3], hvalues[4]); 14 | printf("%d %d %d %d %d\n", hvalues[5], hvalues[6], hvalues[7], hvalues[8], hvalues[9]); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /java/test/test_inner_product.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_inner_product 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector d_vec1 = new DVVector(new float[] { 1.0f, 2.0f, 5.0f }); 9 | DVVector d_vec2 = new DVVector(new float[] { 4.0f, 1.0f, 5.0f }); 10 | System.out.println(TRTC.Inner_Product(d_vec1, d_vec2, new DVFloat(0.0f))); 11 | System.out.println(TRTC.Inner_Product(d_vec1, d_vec2, new DVFloat(0.0f), new Functor("Plus"), new Functor("Multiplies"))); 12 | } 13 | } -------------------------------------------------------------------------------- /python/ThrustRTC/DVTuple.py: -------------------------------------------------------------------------------- 1 | from .Native import ffi, native, check_cptr 2 | from .DeviceViewable import DeviceViewable 3 | from .utils import * 4 | 5 | class DVTuple(DeviceViewable): 6 | def __init__(self, elem_map): 7 | self.m_elem_map = elem_map 8 | param_names = [param_name for param_name, elem in elem_map.items()] 9 | o_param_names = StrArray(param_names) 10 | elems = [elem for param_name, elem in elem_map.items()] 11 | o_elems = ObjArray(elems) 12 | self.m_cptr = check_cptr(native.n_dvtuple_create(o_elems.m_cptr, o_param_names.m_cptr)) 13 | 14 | -------------------------------------------------------------------------------- /cpp/test/test_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "sequence.h" 5 | #include "count.h" 6 | 7 | int main() 8 | { 9 | int hin[2000]; 10 | for (int i = 0; i < 2000; i++) 11 | hin[i] = i % 100; 12 | 13 | DVVector din("int32_t", 2000, hin); 14 | size_t c; 15 | TRTC_Count(din, DVInt32(47), c); 16 | printf("%d\n", (int)c); 17 | 18 | TRTC_Sequence(din); 19 | Functor op = {{},{ "x" }, " return (x%100)==47;\n" }; 20 | TRTC_Count_If(din, op, c); 21 | printf("%d\n", (int)c); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /copy.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_copy_h 2 | #define _TRTC_copy_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out); 11 | 12 | uint32_t THRUST_RTC_API TRTC_Copy_If(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& pred); 13 | uint32_t THRUST_RTC_API TRTC_Copy_If_Stencil(const DVVectorLike& vec_in, const DVVectorLike& vec_stencil, DVVectorLike& vec_out, const Functor& pred); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /inner_product.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_inner_product_h 2 | #define _TRTC_inner_product_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Inner_Product(const DVVectorLike& vec1, const DVVectorLike& vec2, const DeviceViewable& init, ViewBuf& ret); 11 | bool THRUST_RTC_API TRTC_Inner_Product(const DVVectorLike& vec1, const DVVectorLike& vec2, const DeviceViewable& init, ViewBuf& ret, const Functor& binary_op1, const Functor& binary_op2); 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /java/DVRange.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVRange extends DVVectorLike 4 | { 5 | 6 | public DVRange(DVVectorLike src, int begin, int end) 7 | { 8 | super(Native.dvrange_create(src.cptr(), begin, end)); 9 | m_vec_src = src; 10 | } 11 | 12 | public DVRange(DVVectorLike src) 13 | { 14 | super(Native.dvrange_create(src.cptr(), 0, -1)); 15 | m_vec_src = src; 16 | } 17 | 18 | private DVVectorLike m_vec_src; 19 | 20 | @Override 21 | public void close() 22 | { 23 | m_vec_src = null; 24 | super.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/test/test_permutation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_permutation 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector dvalues = new DVVector(new float[] { 10.0f, 20.0f, 30.0f, 40.0f, 50.0f, 60.0f, 70.0f, 80.0f }); 9 | DVVector dindices = new DVVector(new int[] { 2, 6, 1, 3 }); 10 | DVPermutation src = new DVPermutation(dvalues, dindices); 11 | DVVector dst = new DVVector("float", 4); 12 | TRTC.Copy(src, dst); 13 | System.out.println(Arrays.toString((float[])dst.to_host())); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /cpp/test/test_reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVReverse.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | int hinput[4] = { 3, 7, 2, 5 }; 10 | DVVector dinput("int32_t", 4, hinput); 11 | 12 | int houtput[4]; 13 | DVVector doutput("int32_t", 4); 14 | 15 | DVReverse dreverse(dinput); 16 | 17 | TRTC_Transform(dreverse, doutput, Functor("Negate")); 18 | doutput.to_host(houtput); 19 | printf("%d %d %d %d\n", houtput[0], houtput[1], houtput[2], houtput[3]); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /java/test/test_count.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_count 5 | { 6 | public static void main(String[] args) 7 | { 8 | int[] hin=new int[2000]; 9 | for (int i = 0; i < 2000; i++) 10 | hin[i] = i % 100; 11 | 12 | DVVector din = new DVVector(hin); 13 | System.out.println(TRTC.Count(din, new DVInt32(47))); 14 | 15 | TRTC.Sequence(din); 16 | Functor op = new Functor(new String[]{ "x" }, " return (x%100)==47;\n" ); 17 | System.out.println(TRTC.Count_If(din, op)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/test/test_transform_iter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_transform_iter 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector dvalues = new DVVector(new float[] { 1.0f, 4.0f, 9.0f, 16.0f }); 9 | Functor square_root = new Functor(new String[] { "x" }, " return sqrtf(x);\n"); 10 | DVTransform src = new DVTransform(dvalues, "float", square_root); 11 | DVVector dst = new DVVector("float", 4); 12 | TRTC.Copy(src, dst); 13 | System.out.println(Arrays.toString((float[])dst.to_host())); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /transform_scan.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_transform_scan_h 2 | #define _TRTC_transform_scan_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Transform_Inclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& unary_op, const Functor& binary_op); 11 | bool THRUST_RTC_API TRTC_Transform_Exclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& unary_op, const DeviceViewable& init, const Functor& binary_op); 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /java/test/test_mismatch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_mismatch 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector d1 = new DVVector(new int[] { 0, 5, 3, 7 }); 10 | DVVector d2 = new DVVector(new int[] { 0, 5, 8, 7 }); 11 | System.out.println(TRTC.Mismatch(d1, d2)); 12 | } 13 | 14 | { 15 | DVVector d1 = new DVVector(new int[] { 0, 5, 3, 7 }); 16 | DVVector d2 = new DVVector(new int[] { 0, 5, 8, 7 }); 17 | System.out.println(TRTC.Mismatch(d1, d2, new Functor("EqualTo"))); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /python/ThrustRTC/Merging.py: -------------------------------------------------------------------------------- 1 | from .Native import ffi, native, check_i 2 | 3 | def Merge(vec1, vec2, vec_out, comp = None): 4 | cptr_comp = ffi.NULL 5 | if comp!=None: 6 | cptr_comp = comp.m_cptr 7 | check_i(native.n_merge(vec1.m_cptr, vec2.m_cptr, vec_out.m_cptr, cptr_comp)) 8 | 9 | def Merge_By_Key(keys1, keys2, value1, value2, keys_out, value_out, comp = None): 10 | cptr_comp = ffi.NULL 11 | if comp!=None: 12 | cptr_comp = comp.m_cptr 13 | check_i(native.n_merge_by_key(keys1.m_cptr, keys2.m_cptr, value1.m_cptr, value2.m_cptr, keys_out.m_cptr, value_out.m_cptr, cptr_comp)) 14 | 15 | -------------------------------------------------------------------------------- /java/test/test_custom_vector.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_custom_vector 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector d_in = new DVVector(new int[] { 0, 1, 2, 3, 4 }); 9 | DVCustomVector src = new DVCustomVector(new DeviceViewable[] { d_in }, new String[] { "src" }, "idx", 10 | " return src[idx % src.size()];\n", "int32_t", d_in.size() * 5, true); 11 | DVVector dst= new DVVector("int32_t", 25); 12 | TRTC.Copy(src, dst); 13 | System.out.println(Arrays.toString((int[])dst.to_host())); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /java/DVPermutation.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVPermutation extends DVVectorLike 4 | { 5 | public DVPermutation(DVVectorLike vec_value, DVVectorLike vec_index) 6 | { 7 | super(Native.dvpermutation_create(vec_value.cptr(), vec_index.cptr())); 8 | m_vec_value = vec_value; 9 | m_vec_index = vec_index; 10 | } 11 | 12 | private DVVectorLike m_vec_value; 13 | private DVVectorLike m_vec_index; 14 | 15 | @Override 16 | public void close() 17 | { 18 | m_vec_value = null; 19 | m_vec_index = null; 20 | super.close(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /java/test/test_sequence.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_sequence 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector vec= new DVVector("int32_t", 10); 9 | 10 | TRTC.Sequence(vec); 11 | System.out.println(Arrays.toString((int[])vec.to_host())); 12 | 13 | TRTC.Sequence(vec, new DVInt32(1)); 14 | System.out.println(Arrays.toString((int[])vec.to_host())); 15 | 16 | TRTC.Sequence(vec, new DVInt32(1), new DVInt32(3)); 17 | System.out.println(Arrays.toString((int[])vec.to_host())); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /CSharp/test/test_transform_reduce/test_transform_reduce.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_transform_reduce 5 | { 6 | class test_transform_reduce 7 | { 8 | static void Main(string[] args) 9 | { 10 | Functor absolute_value = new Functor ( new string[]{ "x" }, " return x<(decltype(x))0 ? -x : x;\n" ); 11 | DVVector d_data = new DVVector(new int[] { -1, 0, -2, -2, 1, -3 }); 12 | Console.WriteLine(TRTC.Transform_Reduce(d_data, absolute_value, new DVInt32(0), new Functor("Maximum"))); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /fake_vectors/DVZipped.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVZipped_h 2 | #define _DVZipped_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVZipped : public DVVectorLike 7 | { 8 | public: 9 | DVZipped(const std::vector& vecs, const std::vector& elem_names); 10 | virtual ViewBuf view() const; 11 | virtual bool is_readable() const { return m_readable; } 12 | virtual bool is_writable() const { return m_writable; } 13 | 14 | private: 15 | bool m_readable; 16 | bool m_writable; 17 | std::vector m_view_elems; 18 | std::vector m_offsets; 19 | }; 20 | 21 | #endif 22 | 23 | 24 | -------------------------------------------------------------------------------- /python/test/test_find.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | d_values = trtc.device_vector_from_list([0, 5, 3, 7], 'int32_t') 6 | print(trtc.Find(d_values, trtc.DVInt32(3))) 7 | print(trtc.Find(d_values,trtc.DVInt32(5))) 8 | print(trtc.Find(d_values,trtc.DVInt32(9))) 9 | print(trtc.Find_If(d_values, trtc.Functor({}, ['x'], ' return x>4;\n'))) 10 | print(trtc.Find_If(d_values, trtc.Functor({}, ['x'], ' return x>10;\n'))) 11 | print(trtc.Find_If_Not(d_values, trtc.Functor({}, ['x'], ' return x>4;\n'))) 12 | print(trtc.Find_If_Not(d_values, trtc.Functor({}, ['x'], ' return x>10;\n'))) 13 | -------------------------------------------------------------------------------- /python/api_DVTuple.cpp: -------------------------------------------------------------------------------- 1 | #include "api.h" 2 | #include "TRTCContext.h" 3 | #include "DVTuple.h" 4 | 5 | typedef std::vector StrArray; 6 | typedef std::vector PtrArray; 7 | 8 | void* n_dvtuple_create(void* ptr_dvs, void* ptr_names) 9 | { 10 | PtrArray* dvs = (PtrArray*)ptr_dvs; 11 | StrArray* names = (StrArray*)ptr_names; 12 | size_t num_params = dvs->size(); 13 | std::vector arg_map(num_params); 14 | for (size_t i = 0; i < num_params; i++) 15 | { 16 | arg_map[i].obj_name = (*names)[i].c_str(); 17 | arg_map[i].obj = (*dvs)[i]; 18 | } 19 | return new DVTuple(arg_map); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /CSharp/test/test_inner_product/test_inner_product.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_inner_product 5 | { 6 | class test_inner_product 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector d_vec1 = new DVVector(new float[] { 1.0f, 2.0f, 5.0f }); 11 | DVVector d_vec2 = new DVVector(new float[] { 4.0f, 1.0f, 5.0f }); 12 | Console.WriteLine(TRTC.Inner_Product(d_vec1, d_vec2, new DVFloat(0.0f))); 13 | Console.WriteLine(TRTC.Inner_Product(d_vec1, d_vec2, new DVFloat(0.0f), new Functor("Plus"), new Functor("Multiplies"))); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /fake_vectors/DVReverse.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVReverse_h 2 | #define _DVReverse_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVReverse : public DVVectorLike 7 | { 8 | public: 9 | std::string cls_value() const { return m_cls_value; } 10 | ViewBuf view_value() const { return m_view_value; } 11 | 12 | DVReverse(const DVVectorLike& vec_value); 13 | virtual ViewBuf view() const; 14 | virtual bool is_readable() const { return m_readable; } 15 | virtual bool is_writable() const { return m_writable; } 16 | 17 | private: 18 | bool m_readable; 19 | bool m_writable; 20 | std::string m_cls_value; 21 | ViewBuf m_view_value; 22 | }; 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /gather.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_gather_h 2 | #define _TRTC_gather_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Gather(const DVVectorLike& vec_map, const DVVectorLike& vec_in, DVVectorLike& vec_out); 11 | bool THRUST_RTC_API TRTC_Gather_If(const DVVectorLike& vec_map, const DVVectorLike& vec_stencil, const DVVectorLike& vec_in, DVVectorLike& vec_out); 12 | bool THRUST_RTC_API TRTC_Gather_If(const DVVectorLike& vec_map, const DVVectorLike& vec_stencil, const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& pred); 13 | 14 | #endif 15 | 16 | -------------------------------------------------------------------------------- /cpp/test/test_mismatch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "mismatch.h" 5 | 6 | int main() 7 | { 8 | { 9 | int h1[4] = { 0, 5, 3, 7 }; 10 | DVVector d1("int32_t", 4, h1); 11 | int h2[4] = { 0, 5, 8, 7 }; 12 | DVVector d2("int32_t", 4, h2); 13 | size_t res; 14 | TRTC_Mismatch(d1, d2, res); 15 | printf("%d\n", (int)res); 16 | } 17 | 18 | { 19 | int h1[4] = { 0, 5, 3, 7 }; 20 | DVVector d1("int32_t", 4, h1); 21 | int h2[4] = { 0, 5, 8, 7 }; 22 | DVVector d2("int32_t", 4, h2); 23 | size_t res; 24 | TRTC_Mismatch(d1, d2, Functor("EqualTo"), res); 25 | printf("%d\n", (int)res); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /python/ThrustRTC/__init__.py: -------------------------------------------------------------------------------- 1 | from .Native import native, check_i 2 | 3 | check_i(native.n_trtc_try_init()) 4 | 5 | from .Context import * 6 | from .DeviceViewable import * 7 | from .DVVector import device_vector, device_vector_from_numpy, device_vector_from_list, device_vector_from_dvs, DVNumbaVector, DVCupyVector 8 | from .DVTuple import * 9 | from .FakeVectors import * 10 | from .Functor import * 11 | from .Transformations import * 12 | from .Copying import * 13 | from .Reductions import * 14 | from .PrefixSums import * 15 | from .Reordering import * 16 | from .Searching import * 17 | from .Merging import * 18 | from .Sorting import * 19 | 20 | __version__ = '0.3.20' 21 | -------------------------------------------------------------------------------- /scatter.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_scatter_h 2 | #define _TRTC_scatter_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Scatter(const DVVectorLike& vec_in, const DVVectorLike& vec_map, DVVectorLike& vec_out); 11 | bool THRUST_RTC_API TRTC_Scatter_If(const DVVectorLike& vec_in, const DVVectorLike& vec_map, const DVVectorLike& vec_stencil, DVVectorLike& vec_out); 12 | bool THRUST_RTC_API TRTC_Scatter_If(const DVVectorLike& vec_in, const DVVectorLike& vec_map, const DVVectorLike& vec_stencil, DVVectorLike& vec_out, const Functor& pred); 13 | 14 | #endif 15 | 16 | -------------------------------------------------------------------------------- /fake_vectors/DVReverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DVReverse.h" 3 | 4 | DVReverse::DVReverse(const DVVectorLike& vec_value) 5 | : DVVectorLike(vec_value.name_elem_cls().c_str(), vec_value.name_ref_type().c_str(), vec_value.size()) 6 | { 7 | m_readable = vec_value.is_readable(); 8 | m_writable = vec_value.is_writable(); 9 | m_cls_value = vec_value.name_view_cls(); 10 | m_view_value = vec_value.view(); 11 | 12 | m_name_view_cls = std::string("ReverseView<") + m_cls_value + ">"; 13 | } 14 | 15 | ViewBuf DVReverse::view() const 16 | { 17 | ViewBuf buf(m_view_value.size()); 18 | memcpy(buf.data(), m_view_value.data(), m_view_value.size()); 19 | return buf; 20 | } 21 | -------------------------------------------------------------------------------- /transform_reduce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "transform_reduce.h" 3 | #include "general_reduce.h" 4 | 5 | bool TRTC_Transform_Reduce(const DVVectorLike& vec, const Functor& unary_op, const DeviceViewable& init, const Functor& binary_op, ViewBuf& ret) 6 | { 7 | Functor src({ {"vec", &vec}, { "init", &init }, {"unary_op", &unary_op} }, { "idx" }, 8 | " return idx>0?unary_op(vec[idx - 1]):init;\n"); 9 | size_t ret_size = TRTC_Size_Of(init.name_view_cls().c_str()); 10 | ret.resize(ret_size); 11 | memset(ret.data(), 0, ret_size); 12 | if (!general_reduce(vec.size()+1, init.name_view_cls().c_str(), src, binary_op, ret)) return false; 13 | return true; 14 | } 15 | -------------------------------------------------------------------------------- /python/test/test_equal.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | compare_modulo_two = trtc.Functor( {}, ['x','y'], 6 | ''' 7 | return (x % 2) == (y % 2); 8 | ''') 9 | 10 | darr1 = trtc.device_vector_from_list([ 3, 1, 4, 1, 5, 9, 3], 'int32_t') 11 | darr2 = trtc.device_vector_from_list([ 3, 1, 4, 2, 8, 5, 7], 'int32_t') 12 | darr3 = trtc.device_vector_from_list([ 3, 1, 4, 1, 5, 9, 3], 'int32_t') 13 | print(trtc.Equal(darr1, darr2)) 14 | print(trtc.Equal(darr1, darr3)) 15 | 16 | dx = trtc.device_vector_from_list([ 1, 2, 3, 4, 5, 6 ], 'int32_t') 17 | dy = trtc.device_vector_from_list([ 7, 8, 9, 10, 11, 12 ], 'int32_t') 18 | print(trtc.Equal(dx, dy, compare_modulo_two)) 19 | -------------------------------------------------------------------------------- /java/DVTuple.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVTuple extends DeviceViewable 4 | { 5 | private static long create(DeviceViewable[] objs, String[] name_objs) 6 | { 7 | long[] p_objs = new long[objs.length]; 8 | for (int i = 0; i 2 | #include "fake_vectors/DVCounter.h" 3 | 4 | DVCounter::DVCounter(const DeviceViewable& dvobj_init, size_t size) : 5 | DVVectorLike(dvobj_init.name_view_cls().c_str(), dvobj_init.name_view_cls().c_str(), size) 6 | { 7 | m_value_init = dvobj_init.view(); 8 | m_name_view_cls = std::string("CounterView<") + m_elem_cls + ">"; 9 | TRTC_Query_Struct(m_name_view_cls.c_str(), { "_size", "_value_init" }, m_offsets); 10 | } 11 | 12 | ViewBuf DVCounter::view() const 13 | { 14 | ViewBuf buf(m_offsets[2]); 15 | *(size_t*)(buf.data() + m_offsets[0]) = m_size; 16 | memcpy(buf.data() + m_offsets[1], m_value_init.data(), m_value_init.size());; 17 | return buf; 18 | } -------------------------------------------------------------------------------- /cpp/test/test_transform_iter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVTransform.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | 10 | 11 | Functor square_root{ {}, { "x" }, " return sqrtf(x);\n" }; 12 | 13 | float hvalues[8] = { 1.0f, 4.0f, 9.0f, 16.0f }; 14 | DVVector dvalues("float", 4, hvalues); 15 | 16 | float houtput[4]; 17 | DVVector doutput("float", 4); 18 | 19 | DVTransform dtrans(dvalues, "float", square_root); 20 | 21 | TRTC_Transform(dtrans, doutput, Functor("Negate")); 22 | doutput.to_host(houtput); 23 | printf("%f %f %f %f\n", houtput[0], houtput[1], houtput[2], houtput[3]); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /fake_vectors/DVConstant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "fake_vectors/DVConstant.h" 3 | 4 | DVConstant::DVConstant(const DeviceViewable& dvobj, size_t size) : 5 | DVVectorLike(dvobj.name_view_cls().c_str(), (std::string("const ") + dvobj.name_view_cls() + "&").c_str(), size) 6 | { 7 | m_value = dvobj.view(); 8 | m_name_view_cls = std::string("ConstantView<") + m_elem_cls + ">"; 9 | TRTC_Query_Struct(m_name_view_cls.c_str(), { "_size", "_value" }, m_offsets); 10 | } 11 | 12 | ViewBuf DVConstant::view() const 13 | { 14 | ViewBuf buf(m_offsets[2]); 15 | *(size_t*)(buf.data() + m_offsets[0]) = m_size; 16 | memcpy(buf.data() + m_offsets[1], m_value.data(), m_value.size());; 17 | return buf; 18 | } 19 | -------------------------------------------------------------------------------- /java/DVZipped.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVZipped extends DVVectorLike 4 | { 5 | private static long create(DVVectorLike[] vecs, String[] elem_names) 6 | { 7 | long[] p_vecs = new long[vecs.length]; 8 | for (int i = 0; i < vecs.length; i++) 9 | p_vecs[i] = vecs[i].cptr(); 10 | return Native.dvzipped_create(p_vecs, elem_names); 11 | } 12 | 13 | public DVZipped(DVVectorLike[] vecs, String[] elem_names) 14 | { 15 | super(create(vecs, elem_names)); 16 | m_vecs = vecs; 17 | } 18 | 19 | private DVVectorLike[] m_vecs; 20 | 21 | @Override 22 | public void close() 23 | { 24 | m_vecs = null; 25 | super.close(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /mismatch.cpp: -------------------------------------------------------------------------------- 1 | #include "mismatch.h" 2 | #include "general_find.h" 3 | 4 | bool TRTC_Mismatch(const DVVectorLike& vec1, const DVVectorLike& vec2, size_t& result) 5 | { 6 | Functor src({ {"vec1", &vec1}, {"vec2", &vec2}}, { "id" }, " return vec1[id]!=vec2[id];\n"); 7 | if (!general_find(vec1.size(), src, result)) return false; 8 | return true; 9 | } 10 | 11 | bool TRTC_Mismatch(const DVVectorLike& vec1, const DVVectorLike& vec2, const Functor& pred, size_t& result) 12 | { 13 | Functor src({ {"vec1", &vec1}, {"vec2", &vec2}, {"pred", &pred} }, { "id" }, " return !pred(vec1[id],vec2[id]);\n"); 14 | if (!general_find(vec1.size(), src, result)) return false; 15 | return true; 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /python/test/test_scan_by_key.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | d_keys = trtc.device_vector_from_list([0, 0, 0, 1, 1, 2, 3, 3, 3, 3], 'int32_t') 4 | 5 | d_values = trtc.device_vector_from_list([1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], 'int32_t') 6 | trtc.Inclusive_Scan_By_Key(d_keys, d_values, d_values) 7 | print (d_values.to_host()) 8 | 9 | d_values = trtc.device_vector_from_list([1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], 'int32_t') 10 | trtc.Exclusive_Scan_By_Key(d_keys, d_values, d_values) 11 | print (d_values.to_host()) 12 | 13 | d_values = trtc.device_vector_from_list([1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], 'int32_t') 14 | trtc.Exclusive_Scan_By_Key(d_keys, d_values, d_values, trtc.DVInt32(5)) 15 | print (d_values.to_host()) 16 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/DVTupleCLR.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ThrustRTCLR.h" 3 | #include "DVTuple.h" 4 | 5 | namespace ThrustRTCLR 6 | { 7 | template 8 | inline T* just_cast_it(IntPtr p) 9 | { 10 | return (T*)(void*)p; 11 | } 12 | 13 | IntPtr Native::dvtuple_create(array^ p_elem_map) 14 | { 15 | int num_params = p_elem_map->Length; 16 | std::vector arg_map(num_params); 17 | for (int i = 0; i < num_params; i++) 18 | { 19 | arg_map[i].obj_name = just_cast_it(p_elem_map[i].obj_name); 20 | arg_map[i].obj = just_cast_it(p_elem_map[i].obj); 21 | } 22 | return (IntPtr)(new DVTuple(arg_map)); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /CSharp/test/test_count/test_count.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_count 5 | { 6 | class test_count 7 | { 8 | static void Main(string[] args) 9 | { 10 | int[] hin=new int[2000]; 11 | for (int i = 0; i < 2000; i++) 12 | hin[i] = i % 100; 13 | 14 | DVVector din = new DVVector(hin); 15 | Console.WriteLine(TRTC.Count(din, new DVInt32(47))); 16 | 17 | TRTC.Sequence(din); 18 | Functor op = new Functor(new string[]{ "x" }, " return (x%100)==47;\n" ); 19 | Console.WriteLine(TRTC.Count_If(din, op)); 20 | 21 | } 22 | 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CSharp/test/test_fill/test_fill.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_fill 5 | { 6 | class test_fill 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector vec_to_fill = new DVVector("int32_t", 5); 11 | TRTC.Fill(vec_to_fill, new DVInt32(123)); 12 | print_array((int[])vec_to_fill.to_host()); 13 | } 14 | 15 | static void print_array(T[] arr) 16 | { 17 | foreach (var item in arr) 18 | { 19 | Console.Write(item.ToString()); 20 | Console.Write(" "); 21 | } 22 | Console.WriteLine(""); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/test/test_transform_scan.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_transform_scan 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector d_data = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 10 | TRTC.Transform_Inclusive_Scan(d_data, d_data, new Functor("Negate"), new Functor("Plus")); 11 | System.out.println(Arrays.toString((int[])d_data.to_host())); 12 | } 13 | 14 | { 15 | DVVector d_data = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 16 | TRTC.Transform_Exclusive_Scan(d_data, d_data, new Functor("Negate"), new DVInt32(4), new Functor("Plus")); 17 | System.out.println(Arrays.toString((int[])d_data.to_host())); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /cpp/test/test_permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVPermutation.h" 5 | #include "transform.h" 6 | 7 | int main() 8 | { 9 | float hvalues[8] = { 10.0f, 20.0f, 30.0f, 40.0f, 50.0f, 60.0f, 70.0f, 80.0f }; 10 | DVVector dvalues("float", 8, hvalues); 11 | 12 | int hindices[4] = { 2,6,1,3 }; 13 | DVVector dindices("int32_t", 4, hindices); 14 | 15 | float houtput[4]; 16 | DVVector doutput("float", 4); 17 | 18 | DVPermutation perm(dvalues, dindices); 19 | 20 | TRTC_Transform(perm, doutput, Functor("Negate")); 21 | doutput.to_host(houtput); 22 | printf("%f %f %f %f\n", houtput[0], houtput[1], houtput[2], houtput[3]); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /internal/radix_sort.h: -------------------------------------------------------------------------------- 1 | #ifndef _radix_sort_h 2 | #define _radix_sort_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool radix_sort_32(DVVectorLike& vec); 11 | bool radix_sort_reverse_32(DVVectorLike& vec); 12 | bool radix_sort_64(DVVectorLike& vec); 13 | bool radix_sort_reverse_64(DVVectorLike& vec); 14 | 15 | bool radix_sort_by_key_32(DVVectorLike& keys, DVVectorLike& values); 16 | bool radix_sort_by_key_reverse_32(DVVectorLike& keys, DVVectorLike& values); 17 | bool radix_sort_by_key_64(DVVectorLike& keys, DVVectorLike& values); 18 | bool radix_sort_by_key_reverse_64(DVVectorLike& keys, DVVectorLike& values); 19 | 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /python/test/test_logical.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | identity = trtc.Identity() 6 | 7 | darr = trtc.device_vector_from_list([True, True, False], 'bool') 8 | 9 | print(trtc.All_Of(darr.range(0,2), identity)) 10 | print(trtc.All_Of(darr.range(0,3), identity)) 11 | print(trtc.All_Of(darr.range(0,0), identity)) 12 | 13 | print(trtc.Any_Of(darr.range(0,2), identity)) 14 | print(trtc.Any_Of(darr.range(0,3), identity)) 15 | print(trtc.Any_Of(darr.range(2,3), identity)) 16 | print(trtc.Any_Of(darr.range(0,0), identity)) 17 | 18 | print(trtc.None_Of(darr.range(0,2), identity)) 19 | print(trtc.None_Of(darr.range(0,3), identity)) 20 | print(trtc.None_Of(darr.range(2,3), identity)) 21 | print(trtc.None_Of(darr.range(0,0), identity)) 22 | -------------------------------------------------------------------------------- /CSharp/test/test_tabulate/test_tabulate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_tabulate 5 | { 6 | class test_tabulate 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector vec = new DVVector("int32_t", 10); 11 | TRTC.Sequence(vec); 12 | TRTC.Tabulate(vec, new Functor("Negate")); 13 | print_array((int[])vec.to_host()); 14 | } 15 | 16 | static void print_array(T[] arr) 17 | { 18 | foreach (var item in arr) 19 | { 20 | Console.Write(item.ToString()); 21 | Console.Write(" "); 22 | } 23 | Console.WriteLine(""); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /python/test/test_multithread.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | import numpy as np 3 | import threading 4 | 5 | harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32') 6 | darr = trtc.device_vector_from_numpy(harr) 7 | 8 | forLoop = trtc.For(['arr_in','arr_out','k'], "idx", 9 | ''' 10 | arr_out[idx] = arr_in[idx]*k; 11 | ''') 12 | 13 | def thread_func(): 14 | darr_out = trtc.device_vector('float', 5) 15 | forLoop.launch_n(5, [darr, darr_out, trtc.DVFloat(10.0)]) 16 | print (darr_out.to_host()) 17 | 18 | a = threading.Thread(target = thread_func) 19 | b = threading.Thread(target = thread_func) 20 | c = threading.Thread(target = thread_func) 21 | 22 | a.start() 23 | b.start() 24 | c.start() 25 | c.join() 26 | b.join() 27 | a.join() 28 | 29 | 30 | -------------------------------------------------------------------------------- /CSharp/test/test_counter/test_counter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_counter 5 | { 6 | class test_counter 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVCounter src = new DVCounter(new DVInt32(1), 10); 11 | DVVector dst = new DVVector("int32_t", 10); 12 | TRTC.Copy(src, dst); 13 | print_array((int[])dst.to_host()); 14 | } 15 | 16 | static void print_array(T[] arr) 17 | { 18 | foreach (var item in arr) 19 | { 20 | Console.Write(item.ToString()); 21 | Console.Write(" "); 22 | } 23 | Console.WriteLine(""); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /cpp/test/test_swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "swap.h" 5 | 6 | int main() 7 | { 8 | int harr1[8] = { 10, 20, 30, 40, 50, 60, 70, 80 }; 9 | DVVector darr1("int32_t", 8, harr1); 10 | 11 | int harr2[8] = { 1000, 900, 800, 700, 600, 500, 400, 300 }; 12 | DVVector darr2("int32_t", 8, harr2); 13 | 14 | TRTC_Swap(darr1, darr2); 15 | darr1.to_host(harr1); 16 | darr2.to_host(harr2); 17 | 18 | printf("%d %d %d %d ", harr1[0], harr1[1], harr1[2], harr1[3]); 19 | printf("%d %d %d %d\n", harr1[4], harr1[5], harr1[6], harr1[7]); 20 | printf("%d %d %d %d ", harr2[0], harr2[1], harr2[2], harr2[3]); 21 | printf("%d %d %d %d\n", harr2[4], harr2[5], harr2[6], harr2[7]); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /CSharp/test/test_constant/test_constant.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_constant 5 | { 6 | class test_constant 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVConstant src = new DVConstant(new DVInt32(123), 10); 11 | DVVector dst = new DVVector("int32_t", 10); 12 | TRTC.Copy(src, dst); 13 | print_array((int[])dst.to_host()); 14 | } 15 | 16 | static void print_array(T[] arr) 17 | { 18 | foreach (var item in arr) 19 | { 20 | Console.Write(item.ToString()); 21 | Console.Write(" "); 22 | } 23 | Console.WriteLine(""); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /python/test/test_for.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | import numpy as np 3 | 4 | # interface with numpy 5 | harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32') 6 | darr = trtc.device_vector_from_numpy(harr) 7 | 8 | harr2 = np.array([6,7,8,9,10], dtype='int32') 9 | darr2 = trtc.device_vector_from_numpy(harr2) 10 | 11 | # test launching non-templated for 12 | forLoop = trtc.For(['arr_in','arr_out','k'], "idx", 13 | ''' 14 | arr_out[idx] = arr_in[idx]*k; 15 | ''') 16 | 17 | darr_out = trtc.device_vector('float', 5) 18 | forLoop.launch_n(5, [darr, darr_out, trtc.DVFloat(10.0)]) 19 | print (darr_out.to_host()) 20 | 21 | darr_out = trtc.device_vector('int32_t', 5) 22 | forLoop.launch_n(5, [darr2, darr_out, trtc.DVInt32(5)]) 23 | print (darr_out.to_host()) 24 | -------------------------------------------------------------------------------- /fake_vectors/DVCustomVector.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVCustomVector_h 2 | #define _DVCustomVector_h 3 | 4 | #include "TRTC_api.h" 5 | #include "DVVector.h" 6 | #include "TRTCContext.h" 7 | 8 | class THRUST_RTC_API DVCustomVector : public DVVectorLike 9 | { 10 | public: 11 | DVCustomVector(const std::vector& arg_map, const char* name_idx, const char* code_body, 12 | const char* elem_cls, size_t size = (size_t)(-1), bool read_only = true); 13 | 14 | virtual ViewBuf view() const; 15 | 16 | virtual bool is_writable() const { return !m_read_only; } 17 | 18 | private: 19 | size_t m_size; 20 | bool m_read_only; 21 | std::vector m_view_args; 22 | std::vector m_arg_offsets; 23 | size_t m_offsets[3]; 24 | }; 25 | 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /CSharp/test/test_mismatch/test_mismatch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_mismatch 5 | { 6 | class test_mismatch 7 | { 8 | static void Main(string[] args) 9 | { 10 | { 11 | DVVector d1 = new DVVector(new int[] { 0, 5, 3, 7 }); 12 | DVVector d2 = new DVVector(new int[] { 0, 5, 8, 7 }); 13 | Console.WriteLine(TRTC.Mismatch(d1, d2)); 14 | } 15 | 16 | { 17 | DVVector d1 = new DVVector(new int[] { 0, 5, 3, 7 }); 18 | DVVector d2 = new DVVector(new int[] { 0, 5, 8, 7 }); 19 | Console.WriteLine(TRTC.Mismatch(d1, d2, new Functor("EqualTo"))); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /replace.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_replace_h 2 | #define _TRTC_replace_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Replace(DVVectorLike& vec, const DeviceViewable& old_value, const DeviceViewable& new_value); 11 | bool THRUST_RTC_API TRTC_Replace_If(DVVectorLike& vec, const Functor& pred, const DeviceViewable& new_value); 12 | bool THRUST_RTC_API TRTC_Replace_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out, const DeviceViewable& old_value, const DeviceViewable& new_value); 13 | bool THRUST_RTC_API TRTC_Replace_Copy_If(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& pred, const DeviceViewable& new_value); 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /fake_vectors/DVRange.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVRange_h 2 | #define _DVRange_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVRange : public DVVectorLike 7 | { 8 | public: 9 | std::string cls_value() const { return m_cls_value; } 10 | ViewBuf view_value() const { return m_view_value; } 11 | 12 | DVRange(const DVVectorLike& vec_value, size_t begin = 0, size_t end = (size_t)(-1)); 13 | virtual ViewBuf view() const; 14 | virtual bool is_readable() const { return m_readable; } 15 | virtual bool is_writable() const { return m_writable; } 16 | 17 | private: 18 | bool m_readable; 19 | bool m_writable; 20 | std::string m_cls_value; 21 | ViewBuf m_view_value; 22 | size_t m_begin; 23 | size_t m_end; 24 | 25 | size_t m_offsets[4]; 26 | 27 | }; 28 | 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /python/test/test_custom_vector.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector('int32_t', 2000) 6 | trtc.Sequence(darr) 7 | 8 | d_custom_values = trtc.DVCustomVector({'src': darr}, 'idx', 9 | ''' 10 | unsigned group = idx / src.size(); 11 | unsigned sub_idx = idx % src.size(); 12 | return src[sub_idx] % (group+1) ==0 ? 1: 0; 13 | ''', 'uint32_t', darr.size()*10) 14 | 15 | d_custom_keys = trtc.DVCustomVector({'src': darr}, 'idx', 16 | ''' 17 | return idx / src.size(); 18 | ''', 'uint32_t', darr.size()*10) 19 | 20 | d_values_out = trtc.device_vector('uint32_t', 10) 21 | d_keys_out = trtc.DVDiscard('uint32_t', 10) 22 | 23 | trtc.Reduce_By_Key(d_custom_keys, d_custom_values, d_keys_out, d_values_out) 24 | 25 | print(d_values_out.to_host()) 26 | -------------------------------------------------------------------------------- /python/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from codecs import open 3 | import os 4 | 5 | here = os.path.abspath(os.path.dirname(__file__)) 6 | 7 | with open(os.path.join(here, 'README.md'), encoding='utf-8') as f: 8 | long_description = f.read() 9 | 10 | setup( 11 | name = 'ThrustRTC', 12 | version = '0.3.20', 13 | description = 'Thrust for Python based on NVRTC', 14 | long_description=long_description, 15 | long_description_content_type='text/markdown', 16 | url='https://github.com/fynv/ThrustRTC', 17 | license='Anti 996', 18 | author='Fei Yang', 19 | author_email='hyangfeih@gmail.com', 20 | keywords='GPU CUDA Thrust', 21 | packages=['ThrustRTC'], 22 | package_data = { 'ThrustRTC': ['*.dll', '*.so']}, 23 | install_requires = ['cffi','numpy'], 24 | ) 25 | 26 | -------------------------------------------------------------------------------- /fake_vectors/DVTransform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "DVTransform.h" 4 | 5 | DVTransform::DVTransform(const DVVectorLike& vec_in, const char* elem_cls, const Functor& op) 6 | : DVVectorLike(elem_cls, elem_cls, vec_in.size()) 7 | { 8 | m_view_in = vec_in.view(); 9 | m_view_op = op.view(); 10 | m_name_view_cls = std::string("TransformView<") + elem_cls + "," + vec_in.name_view_cls() + "," + op.name_view_cls() + ">"; 11 | TRTC_Query_Struct(m_name_view_cls.c_str(), { "_view_vec_in", "_view_op" }, m_offsets); 12 | } 13 | 14 | ViewBuf DVTransform::view() const 15 | { 16 | ViewBuf buf(m_offsets[2]); 17 | memcpy(buf.data() + m_offsets[0], m_view_in.data(), m_view_in.size()); 18 | memcpy(buf.data() + m_offsets[1], m_view_op.data(), m_view_op.size()); 19 | return buf; 20 | } 21 | -------------------------------------------------------------------------------- /CSharp/test/test_reverse/test_reverse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_reverse 5 | { 6 | class test_reverse 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector dvalues = new DVVector(new int[] { 3, 7, 2, 5 }); 11 | DVReverse src = new DVReverse(dvalues); 12 | DVVector dst = new DVVector("int32_t", 4); 13 | TRTC.Copy(src, dst); 14 | print_array((int[])dst.to_host()); 15 | } 16 | 17 | static void print_array(T[] arr) 18 | { 19 | foreach (var item in arr) 20 | { 21 | Console.Write(item.ToString()); 22 | Console.Write(" "); 23 | } 24 | Console.WriteLine(""); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /find.cpp: -------------------------------------------------------------------------------- 1 | #include "find.h" 2 | #include "general_find.h" 3 | 4 | bool TRTC_Find(const DVVectorLike& vec, const DeviceViewable& value, size_t& result) 5 | { 6 | Functor src({ {"vec", &vec}, {"value", &value} }, { "id" }, " return vec[id]==value;\n"); 7 | return general_find(vec.size(), src, result); 8 | } 9 | 10 | bool TRTC_Find_If(const DVVectorLike& vec, const Functor& pred, size_t& result) 11 | { 12 | Functor src({ {"vec", &vec}, {"pred", &pred} }, { "id" }, " return pred(vec[id]);\n"); 13 | return general_find(vec.size(), src, result); 14 | } 15 | 16 | bool TRTC_Find_If_Not(const DVVectorLike& vec, const Functor& pred, size_t& result) 17 | { 18 | Functor src({ {"vec", &vec}, {"pred", &pred} }, { "id" }, " return !pred(vec[id]);\n"); 19 | return general_find(vec.size(), src, result); 20 | } 21 | -------------------------------------------------------------------------------- /extrema.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_extrema_h 2 | #define _TRTC_extrema_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Min_Element(const DVVectorLike& vec, size_t& id_min); 11 | bool THRUST_RTC_API TRTC_Min_Element(const DVVectorLike& vec, const Functor& comp, size_t& id_min); 12 | bool THRUST_RTC_API TRTC_Max_Element(const DVVectorLike& vec, size_t& id_max); 13 | bool THRUST_RTC_API TRTC_Max_Element(const DVVectorLike& vec, const Functor& comp, size_t& id_max); 14 | bool THRUST_RTC_API TRTC_MinMax_Element(const DVVectorLike& vec, size_t& id_min, size_t& id_max); 15 | bool THRUST_RTC_API TRTC_MinMax_Element(const DVVectorLike& vec, const Functor& comp, size_t& id_min, size_t& id_max); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /cpp/test/test_fill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVRange.h" 5 | #include "fill.h" 6 | 7 | int main() 8 | { 9 | DVVector vec_to_fill("int32_t", 10); 10 | /* 11 | DVRange front(vec_to_fill, 0, 5); 12 | DVRange rear(vec_to_fill, 5, 10);*/ 13 | 14 | DVVectorAdaptor front(vec_to_fill, 0, 5); 15 | DVVectorAdaptor rear(vec_to_fill, 5, 10); 16 | 17 | TRTC_Fill(front, DVInt32(123)); 18 | TRTC_Fill(rear, DVInt32(456)); 19 | 20 | int values_filled[10]; 21 | vec_to_fill.to_host(values_filled); 22 | printf("%d %d %d %d %d ", values_filled[0], values_filled[1], values_filled[2], values_filled[3], values_filled[4]); 23 | printf("%d %d %d %d %d\n", values_filled[5], values_filled[6], values_filled[7], values_filled[8], values_filled[9]); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /python/test/test_scan.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 4 | trtc.Inclusive_Scan(darr, darr) 5 | print (darr.to_host()) 6 | 7 | darr = trtc.device_vector_from_list([-5, 0, 2, -3, 2, 4, 0, -1, 2, 8], 'int32_t') 8 | trtc.Inclusive_Scan(darr, darr, trtc.Maximum()) 9 | print (darr.to_host()) 10 | 11 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 12 | trtc.Exclusive_Scan(darr, darr) 13 | print (darr.to_host()) 14 | 15 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 16 | trtc.Exclusive_Scan(darr, darr, trtc.DVInt32(4)) 17 | print (darr.to_host()) 18 | 19 | darr = trtc.device_vector_from_list([-5, 0, 2, -3, 2, 4, 0, -1, 2, 8], 'int32_t') 20 | trtc.Exclusive_Scan(darr, darr, trtc.DVInt32(1), trtc.Maximum()) 21 | print (darr.to_host()) 22 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCSharp/Sorting.cs: -------------------------------------------------------------------------------- 1 | using ThrustRTCLR; 2 | 3 | namespace ThrustRTCSharp 4 | { 5 | public partial class TRTC 6 | { 7 | public static bool Sort(DVVectorLike vec) 8 | { 9 | return Native.sort(vec.m_cptr); 10 | } 11 | 12 | public static bool Sort(DVVectorLike vec, Functor comp) 13 | { 14 | return Native.sort(vec.m_cptr, comp.m_cptr); 15 | } 16 | 17 | public static bool Sort_By_Key(DVVectorLike keys, DVVectorLike values) 18 | { 19 | return Native.sort_by_key(keys.m_cptr, values.m_cptr); 20 | } 21 | 22 | public static bool Sort_By_Key(DVVectorLike keys, DVVectorLike values, Functor comp) 23 | { 24 | return Native.sort_by_key(keys.m_cptr, values.m_cptr, comp.m_cptr); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/AssemblyInfo.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | using namespace System; 4 | using namespace System::Reflection; 5 | using namespace System::Runtime::CompilerServices; 6 | using namespace System::Runtime::InteropServices; 7 | using namespace System::Security::Permissions; 8 | 9 | [assembly:AssemblyTitleAttribute(L"ThrustRTCLR")]; 10 | [assembly:AssemblyDescriptionAttribute(L"")]; 11 | [assembly:AssemblyConfigurationAttribute(L"")]; 12 | [assembly:AssemblyCompanyAttribute(L"Microsoft")]; 13 | [assembly:AssemblyProductAttribute(L"ThrustRTCLR")]; 14 | [assembly:AssemblyCopyrightAttribute(L"Copyright (c) Microsoft 2019")]; 15 | [assembly:AssemblyTrademarkAttribute(L"")]; 16 | [assembly:AssemblyCultureAttribute(L"")]; 17 | 18 | [assembly:AssemblyVersionAttribute("1.0.*")]; 19 | 20 | [assembly:ComVisible(false)]; 21 | 22 | [assembly:CLSCompliantAttribute(true)]; 23 | -------------------------------------------------------------------------------- /java/test/test_zipped.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_zipped 5 | { 6 | public static void main(String[] args) 7 | { 8 | DVVector d_int_in = new DVVector(new int[] { 0, 1, 2, 3, 4 }); 9 | DVVector d_float_in = new DVVector(new float[] { 0.0f, 10.0f, 20.0f, 30.0f, 40.0f }); 10 | 11 | DVVector d_int_out = new DVVector("int32_t", 5); 12 | DVVector d_float_out = new DVVector("float", 5); 13 | 14 | DVZipped src = new DVZipped(new DVVectorLike[] { d_int_in, d_float_in }, new String[] { "a", "b" }); 15 | DVZipped dst = new DVZipped(new DVVectorLike[] { d_int_out, d_float_out }, new String[] { "a", "b" }); 16 | 17 | TRTC.Copy(src, dst); 18 | 19 | System.out.println(Arrays.toString((int[])d_int_out.to_host())); 20 | System.out.println(Arrays.toString((float[])d_float_out.to_host())); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /python/test/test_copy.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | dIn = trtc.device_vector_from_list([ 10, 20, 30, 40, 50, 60, 70, 80 ], 'int32_t') 6 | dOut = trtc.device_vector('int32_t', 8) 7 | 8 | trtc.Copy(dIn, dOut) 9 | print (dOut.to_host()) 10 | 11 | 12 | is_even = trtc.Functor( {}, ['x'], 13 | ''' 14 | return x % 2 == 0; 15 | ''') 16 | 17 | dIn = trtc.device_vector_from_list([ -2, 0, -1, 0, 1, 2 ], 'int32_t') 18 | dOut = trtc.device_vector('int32_t', 6) 19 | count = trtc.Copy_If(dIn, dOut, is_even) 20 | print (dOut.to_host(0, count)) 21 | 22 | dIn = trtc.device_vector_from_list([ 0, 1, 2, 3, 4, 5 ], 'int32_t') 23 | dStencil = trtc.device_vector_from_list([ -2, 0, -1, 0, 1, 2 ], 'int32_t') 24 | dOut = trtc.device_vector('int32_t', 6) 25 | count = trtc.Copy_If_Stencil(dIn, dStencil, dOut, is_even) 26 | print (dOut.to_host(0, count)) 27 | -------------------------------------------------------------------------------- /cpp/test/test_adjacent_difference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "adjacent_difference.h" 5 | 6 | int main() 7 | { 8 | int hvec1[8] = { 1, 2, 1, 2, 1, 2, 1, 2 }; 9 | DVVector vec1_in("int32_t", 8, hvec1); 10 | DVVector vec1_out("int32_t", 8); 11 | TRTC_Adjacent_Difference(vec1_in, vec1_out); 12 | vec1_out.to_host(hvec1); 13 | printf("%d %d %d %d %d %d %d %d\n", hvec1[0], hvec1[1], hvec1[2], hvec1[3], hvec1[4], hvec1[5], hvec1[6], hvec1[7]); 14 | 15 | int hvec2[8] = { 1, 2, 1, 2, 1, 2, 1, 2 }; 16 | DVVector vec2_in("int32_t", 8, hvec2); 17 | DVVector vec2_out("int32_t", 8); 18 | TRTC_Adjacent_Difference(vec2_in, vec2_out, Functor("Plus")); 19 | vec2_out.to_host(hvec2); 20 | printf("%d %d %d %d %d %d %d %d\n", hvec2[0], hvec2[1], hvec2[2], hvec2[3], hvec2[4], hvec2[5], hvec2[6], hvec2[7]); 21 | } 22 | -------------------------------------------------------------------------------- /java/test/test_adjacent_difference.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_adjacent_difference 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector vec_in = new DVVector(new int[] { 1, 2, 1, 2, 1, 2, 1, 2 }); 10 | DVVector vec_out = new DVVector("int32_t", 8); 11 | TRTC.Adjacent_Difference(vec_in, vec_out); 12 | System.out.println(Arrays.toString((int[])vec_out.to_host())); 13 | } 14 | 15 | { 16 | DVVector vec_in = new DVVector(new int[] { 1, 2, 1, 2, 1, 2, 1, 2 }); 17 | DVVector vec_out = new DVVector("int32_t", 8); 18 | TRTC.Adjacent_Difference(vec_in, vec_out, new Functor("Plus")); 19 | System.out.println(Arrays.toString((int[])vec_out.to_host())); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /CSharp/test/test_swap/test_swap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_swap 5 | { 6 | class test_swap 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector darr1 = new DVVector(new int[] { 10, 20, 30, 40, 50, 60, 70, 80 }); 11 | DVVector darr2 = new DVVector(new int[] { 1000, 900, 800, 700, 600, 500, 400, 300 }); 12 | TRTC.Swap(darr1, darr2); 13 | print_array((int[])darr1.to_host()); 14 | print_array((int[])darr2.to_host()); 15 | } 16 | 17 | static void print_array(T[] arr) 18 | { 19 | foreach (var item in arr) 20 | { 21 | Console.Write(item.ToString()); 22 | Console.Write(" "); 23 | } 24 | Console.WriteLine(""); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /merge.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_merge_h 2 | #define _TRTC_merge_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Merge(const DVVectorLike& vec1, const DVVectorLike& vec2, DVVectorLike& vec_out); 11 | bool THRUST_RTC_API TRTC_Merge(const DVVectorLike& vec1, const DVVectorLike& vec2, DVVectorLike& vec_out, const Functor& comp); 12 | bool THRUST_RTC_API TRTC_Merge_By_Key(const DVVectorLike& keys1, const DVVectorLike& keys2, const DVVectorLike& value1, const DVVectorLike& value2, DVVectorLike& keys_out, DVVectorLike& value_out); 13 | bool THRUST_RTC_API TRTC_Merge_By_Key(const DVVectorLike& keys1, const DVVectorLike& keys2, const DVVectorLike& value1, const DVVectorLike& value2, DVVectorLike& keys_out, DVVectorLike& value_out, const Functor& comp); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /cpp/test/test_equal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "equal.h" 5 | 6 | int main() 7 | { 8 | int harr1[7] = { 3, 1, 4, 1, 5, 9, 3 }; 9 | int harr2[7] = { 3, 1, 4, 2, 8, 5, 7 }; 10 | DVVector darr1("int32_t", 6, harr1); 11 | DVVector darr2("int32_t", 6, harr2); 12 | DVVector darr3("int32_t", 6, harr1); 13 | 14 | bool res1, res2; 15 | TRTC_Equal(darr1, darr2, res1); 16 | TRTC_Equal(darr1, darr3, res2); 17 | 18 | printf("%d %d\n", res1, res2); 19 | 20 | Functor compare_modulo_two = { {},{ "x", "y" }, " return (x % 2) == (y % 2);\n" }; 21 | int x[6] = { 1, 2, 3, 4, 5, 6 }; 22 | int y[6] = { 7, 8, 9, 10, 11, 12 }; 23 | DVVector dx("int32_t", 6, x); 24 | DVVector dy("int32_t", 6, y); 25 | bool res3; 26 | TRTC_Equal(dx, dy, compare_modulo_two, res3); 27 | 28 | printf("%d\n", res3); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /cpp/test/test_transform_scan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "fake_vectors/DVCounter.h" 5 | #include "transform_scan.h" 6 | 7 | int main() 8 | { 9 | { 10 | int data[6] = { 1, 0, 2, 2, 1, 3 }; 11 | DVVector d_data("int32_t", 6, data); 12 | 13 | TRTC_Transform_Inclusive_Scan(d_data, d_data, Functor("Negate"), Functor("Plus")); 14 | 15 | d_data.to_host(data); 16 | printf("%d %d %d %d %d %d\n", data[0], data[1], data[2], data[3], data[4], data[5]); 17 | } 18 | 19 | { 20 | int data[6] = { 1, 0, 2, 2, 1, 3 }; 21 | DVVector d_data("int32_t", 6, data); 22 | 23 | TRTC_Transform_Exclusive_Scan(d_data, d_data, Functor("Negate"), DVInt32(4), Functor("Plus")); 24 | 25 | d_data.to_host(data); 26 | printf("%d %d %d %d %d %d\n", data[0], data[1], data[2], data[3], data[4], data[5]); 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /cpp/test/test_inner_product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "inner_product.h" 5 | 6 | int main() 7 | { 8 | { 9 | float h_vec1[3] = { 1.0f, 2.0f, 5.0f }; 10 | float h_vec2[3] = { 4.0f, 1.0f, 5.0f }; 11 | 12 | DVVector d_vec1("float", 3, h_vec1); 13 | DVVector d_vec2("float", 3, h_vec2); 14 | 15 | ViewBuf res; 16 | TRTC_Inner_Product(d_vec1, d_vec2, DVFloat(0.0f), res); 17 | printf("%f\n", *(float*)res.data()); 18 | } 19 | 20 | { 21 | float h_vec1[3] = { 1.0f, 2.0f, 5.0f }; 22 | float h_vec2[3] = { 4.0f, 1.0f, 5.0f }; 23 | 24 | DVVector d_vec1("float", 3, h_vec1); 25 | DVVector d_vec2("float", 3, h_vec2); 26 | 27 | ViewBuf res; 28 | TRTC_Inner_Product(d_vec1, d_vec2, DVFloat(0.0f), res, Functor("Plus"), Functor("Multiplies")); 29 | printf("%f\n", *(float*)res.data()); 30 | 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /fake_vectors/DVPermutation.h: -------------------------------------------------------------------------------- 1 | #ifndef _DVPermutation_h 2 | #define _DVPermutation_h 3 | 4 | #include "DVVector.h" 5 | 6 | class THRUST_RTC_API DVPermutation : public DVVectorLike 7 | { 8 | public: 9 | std::string cls_value() const { return m_cls_value; } 10 | ViewBuf view_value() const { return m_view_value; } 11 | std::string cls_index() const { return m_cls_index; } 12 | ViewBuf view_index() const { return m_view_index; } 13 | 14 | DVPermutation(const DVVectorLike& vec_value, const DVVectorLike& vec_index ); 15 | virtual ViewBuf view() const; 16 | virtual bool is_readable() const { return m_readable; } 17 | virtual bool is_writable() const { return m_writable; } 18 | 19 | private: 20 | bool m_readable; 21 | bool m_writable; 22 | std::string m_cls_value; 23 | ViewBuf m_view_value; 24 | std::string m_cls_index; 25 | ViewBuf m_view_index; 26 | 27 | size_t m_offsets[3]; 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /remove.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_remove_h 2 | #define _TRTC_remove_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | uint32_t THRUST_RTC_API TRTC_Remove(DVVectorLike& vec, const DeviceViewable& value); 11 | uint32_t THRUST_RTC_API TRTC_Remove_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out, const DeviceViewable& value); 12 | uint32_t THRUST_RTC_API TRTC_Remove_If(DVVectorLike& vec, const Functor& pred); 13 | uint32_t THRUST_RTC_API TRTC_Remove_Copy_If(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& pred); 14 | uint32_t THRUST_RTC_API TRTC_Remove_If_Stencil(DVVectorLike& vec, const DVVectorLike& stencil, const Functor& pred); 15 | uint32_t THRUST_RTC_API TRTC_Remove_Copy_If_Stencil(const DVVectorLike& vec_in, const DVVectorLike& stencil, DVVectorLike& vec_out, const Functor& pred); 16 | 17 | 18 | #endif -------------------------------------------------------------------------------- /java/DVTuple.cpp: -------------------------------------------------------------------------------- 1 | #include "JThrustRTC_Native.h" 2 | #include "DVTuple.h" 3 | 4 | JNIEXPORT jlong JNICALL Java_JThrustRTC_Native_dvtuple_1create(JNIEnv *env, jclass, jlongArray p_objs, jobjectArray j_name_objs) 5 | { 6 | jsize num_objs = env->GetArrayLength(j_name_objs); 7 | jlong* lpobjs = env->GetLongArrayElements(p_objs, nullptr); 8 | 9 | std::vector elem_map(num_objs); 10 | for (int i = 0; i < num_objs; i++) 11 | { 12 | elem_map[i].obj_name = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(j_name_objs, i), nullptr); 13 | elem_map[i].obj = (const DeviceViewable*)lpobjs[i]; 14 | } 15 | 16 | DVTuple* cptr = new DVTuple(elem_map); 17 | 18 | env->ReleaseLongArrayElements(p_objs, lpobjs, 0); 19 | for (int i = 0; i < num_objs; i++) 20 | env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(j_name_objs, i), elem_map[i].obj_name); 21 | 22 | return (jlong)cptr; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /CSharp/test/test_sequence/test_sequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_sequence 5 | { 6 | class test_sequence 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector vec= new DVVector("int32_t", 10); 11 | 12 | TRTC.Sequence(vec); 13 | print_array((int[])vec.to_host()); 14 | 15 | TRTC.Sequence(vec, new DVInt32(1)); 16 | print_array((int[])vec.to_host()); 17 | 18 | TRTC.Sequence(vec, new DVInt32(1), new DVInt32(3)); 19 | print_array((int[])vec.to_host()); 20 | 21 | } 22 | 23 | static void print_array(T[] arr) 24 | { 25 | foreach (var item in arr) 26 | { 27 | Console.Write(item.ToString()); 28 | Console.Write(" "); 29 | } 30 | Console.WriteLine(""); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /sort.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_sort_h 2 | #define _TRTC_sort_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | // all merge-based 11 | bool THRUST_RTC_API TRTC_Sort(DVVectorLike& vec); 12 | bool THRUST_RTC_API TRTC_Sort(DVVectorLike& vec, const Functor& comp); 13 | bool THRUST_RTC_API TRTC_Sort_By_Key(DVVectorLike& keys, DVVectorLike& values); 14 | bool THRUST_RTC_API TRTC_Sort_By_Key(DVVectorLike& keys, DVVectorLike& values, const Functor& comp); 15 | 16 | bool THRUST_RTC_API TRTC_Is_Sorted(const DVVectorLike& vec, bool& result); 17 | bool THRUST_RTC_API TRTC_Is_Sorted(const DVVectorLike& vec, const Functor& comp, bool& result); 18 | bool THRUST_RTC_API TRTC_Is_Sorted_Until(const DVVectorLike& vec, size_t& result); 19 | bool THRUST_RTC_API TRTC_Is_Sorted_Until(const DVVectorLike& vec, const Functor& comp, size_t& result); 20 | 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /java/test/test_equal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_equal 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector darr1 = new DVVector(new int[] { 3, 1, 4, 1, 5, 9, 3 }); 10 | DVVector darr2 = new DVVector(new int[] { 3, 1, 4, 2, 8, 5, 7 }); 11 | DVVector darr3 = new DVVector(new int[] { 3, 1, 4, 1, 5, 9, 3 }); 12 | System.out.println(TRTC.Equal(darr1, darr2).toString() + " " +TRTC.Equal(darr1, darr3).toString()); 13 | } 14 | 15 | { 16 | Functor compare_modulo_two = new Functor( new String[]{ "x", "y" }, " return (x % 2) == (y % 2);\n" ); 17 | DVVector dx = new DVVector(new int[] { 1, 2, 3, 4, 5, 6 }); 18 | DVVector dy = new DVVector(new int[] { 7, 8, 9, 10, 11, 12 }); 19 | System.out.println(TRTC.Equal(dx, dy, compare_modulo_two)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /python/test/test_replace.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | is_less_than_zero = trtc.Functor({}, ['x'], 6 | ''' 7 | return x<0; 8 | ''') 9 | 10 | darr1 = trtc.device_vector_from_list([1,2,3,1,2], 'int32_t') 11 | trtc.Replace(darr1, trtc.DVInt32(1), trtc.DVInt32(99)) 12 | print (darr1.to_host()) 13 | 14 | darr2 = trtc.device_vector_from_list([1, -2, 3, -4, 5 ], 'int32_t') 15 | trtc.Replace_If(darr2, is_less_than_zero , trtc.DVInt32(0)) 16 | print (darr2.to_host()) 17 | 18 | darr3_in = trtc.device_vector_from_list([1,2,3,1,2], 'int32_t') 19 | darr3_out = trtc.device_vector('int32_t', 5) 20 | trtc.Replace_Copy(darr3_in, darr3_out, trtc.DVInt32(1), trtc.DVInt32(99)) 21 | print (darr3_out.to_host()) 22 | 23 | darr4_in = trtc.device_vector_from_list([1, -2, 3, -4, 5 ], 'int32_t') 24 | darr4_out = trtc.device_vector('int32_t', 5) 25 | trtc.Replace_Copy_If(darr4_in, darr4_out, is_less_than_zero, trtc.DVInt32(0)) 26 | print (darr4_out.to_host()) 27 | -------------------------------------------------------------------------------- /CSharp/test/test_permutation/test_permutation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_permutation 5 | { 6 | class test_permutation 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector dvalues = new DVVector(new float[] { 10.0f, 20.0f, 30.0f, 40.0f, 50.0f, 60.0f, 70.0f, 80.0f }); 11 | DVVector dindices = new DVVector(new int[] { 2, 6, 1, 3 }); 12 | DVPermutation src = new DVPermutation(dvalues, dindices); 13 | DVVector dst = new DVVector("float", 4); 14 | TRTC.Copy(src, dst); 15 | print_array((float[])dst.to_host()); 16 | } 17 | 18 | static void print_array(T[] arr) 19 | { 20 | foreach (var item in arr) 21 | { 22 | Console.Write(item.ToString()); 23 | Console.Write(" "); 24 | } 25 | Console.WriteLine(""); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /transform_scan.cpp: -------------------------------------------------------------------------------- 1 | #include "transform_scan.h" 2 | #include "general_scan.h" 3 | 4 | bool TRTC_Transform_Inclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& unary_op, const Functor& binary_op) 5 | { 6 | Functor src({ {"vec_in", &vec_in}, {"unary_op", &unary_op}, {"vec_out", &vec_out} }, { "idx" }, 7 | " return (decltype(vec_out)::value_t)unary_op(vec_in[idx]);\n"); 8 | return general_scan(vec_in.size(), src, vec_out, binary_op); 9 | } 10 | 11 | bool TRTC_Transform_Exclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& unary_op, const DeviceViewable& init, const Functor& binary_op) 12 | { 13 | Functor src({ {"vec_in", &vec_in}, {"unary_op", &unary_op}, {"vec_out", &vec_out}, {"init", &init} }, { "idx" }, 14 | " return idx>0? (decltype(vec_out)::value_t)unary_op(vec_in[idx - 1]): (decltype(vec_out)::value_t)init;\n"); 15 | return general_scan(vec_in.size(), src, vec_out, binary_op); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /partition.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_partition_h 2 | #define _TRTC_partition_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | uint32_t THRUST_RTC_API TRTC_Partition(DVVectorLike& vec, const Functor& pred); 11 | uint32_t THRUST_RTC_API TRTC_Partition_Stencil(DVVectorLike& vec, const DVVectorLike& stencil, const Functor& pred); 12 | uint32_t THRUST_RTC_API TRTC_Partition_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_true, DVVectorLike& vec_false, const Functor& pred); 13 | uint32_t THRUST_RTC_API TRTC_Partition_Copy_Stencil(const DVVectorLike& vec_in, const DVVectorLike& stencil, DVVectorLike& vec_true, DVVectorLike& vec_false, const Functor& pred); 14 | 15 | bool THRUST_RTC_API TRTC_Partition_Point(const DVVectorLike& vec, const Functor& pred, size_t& result); 16 | bool THRUST_RTC_API TRTC_Is_Partitioned(const DVVectorLike& vec, const Functor& pred, bool& result); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /.github/workflows/conda_windows.yml: -------------------------------------------------------------------------------- 1 | name: Conda Windows Publishment 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | paths: 7 | - 'conda_windows/meta.yaml' 8 | 9 | jobs: 10 | build_windows_latest: 11 | runs-on: windows-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | with: 16 | submodules: true 17 | 18 | - name: Set up Python 3.8 19 | uses: conda-incubator/setup-miniconda@v2 20 | with: 21 | python-version: 3.8 22 | 23 | - uses: ilammy/msvc-dev-cmd@v1 24 | 25 | - name: Set Always Yes 26 | run: conda config --set always_yes yes 27 | 28 | - name: Install conda tools 29 | run: conda install conda-build anaconda-client 30 | 31 | - name: Set Auto Upload 32 | run: conda config --set anaconda_upload yes 33 | 34 | - name: conda build and release 35 | run: conda-build conda_windows --no-test --user fyplus --token ${{ secrets.ANACONDA_TOKEN }} 36 | -------------------------------------------------------------------------------- /java/DVCustomVector.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVCustomVector extends DVVectorLike 4 | { 5 | private static long create(DeviceViewable[] objs, String[] name_objs, String name_idx, String code_body, String elem_cls, int size, boolean read_only) 6 | { 7 | long[] p_objs = new long[objs.length]; 8 | for (int i = 0; i(T[] arr) 19 | { 20 | foreach (var item in arr) 21 | { 22 | Console.Write(item.ToString()); 23 | Console.Write(" "); 24 | } 25 | Console.WriteLine(""); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CSharp/test/test_transform_iter/test_transform_iter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_transform_iter 5 | { 6 | class test_transform_iter 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector dvalues = new DVVector(new float[] { 1.0f, 4.0f, 9.0f, 16.0f }); 11 | Functor square_root = new Functor(new string[] { "x" }, " return sqrtf(x);\n"); 12 | DVTransform src = new DVTransform(dvalues, "float", square_root); 13 | DVVector dst = new DVVector("float", 4); 14 | TRTC.Copy(src, dst); 15 | print_array((float[])dst.to_host()); 16 | 17 | } 18 | 19 | static void print_array(T[] arr) 20 | { 21 | foreach (var item in arr) 22 | { 23 | Console.Write(item.ToString()); 24 | Console.Write(" "); 25 | } 26 | Console.WriteLine(""); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /transform.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_transform_h 2 | #define _TRTC_transform_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Transform(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& op); 11 | bool THRUST_RTC_API TRTC_Transform_Binary(const DVVectorLike& vec_in1, const DVVectorLike& vec_in2, DVVectorLike& vec_out, const Functor& op); 12 | bool THRUST_RTC_API TRTC_Transform_If(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& op, const Functor& pred); 13 | bool THRUST_RTC_API TRTC_Transform_If_Stencil(const DVVectorLike& vec_in, const DVVectorLike& vec_stencil, DVVectorLike& vec_out, const Functor& op, const Functor& pred); 14 | bool THRUST_RTC_API TRTC_Transform_Binary_If_Stencil(const DVVectorLike& vec_in1, const DVVectorLike& vec_in2, const DVVectorLike& vec_stencil, DVVectorLike& vec_out, const Functor& op, const Functor& pred); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /DVTuple.cpp: -------------------------------------------------------------------------------- 1 | #include "memory.h" 2 | #include "DVTuple.h" 3 | 4 | DVTuple::DVTuple(const std::vector& elem_map) 5 | { 6 | std::string struct_body; 7 | m_view_elems.resize(elem_map.size()); 8 | std::vector name_elems(elem_map.size()); 9 | for (size_t i = 0; i < elem_map.size(); i++) 10 | { 11 | struct_body += std::string(" ") + elem_map[i].obj->name_view_cls() + " " + elem_map[i].obj_name + ";\n"; 12 | m_view_elems[i] = elem_map[i].obj->view(); 13 | name_elems[i] = elem_map[i].obj_name; 14 | } 15 | 16 | m_name_view_cls = TRTC_Add_Struct(struct_body.c_str()); 17 | m_offsets.resize(elem_map.size() + 1); 18 | TRTC_Query_Struct(m_name_view_cls.c_str(), name_elems, m_offsets.data()); 19 | } 20 | 21 | ViewBuf DVTuple::view() const 22 | { 23 | ViewBuf ret(m_offsets[m_offsets.size() - 1]); 24 | for (size_t i = 0; i < m_view_elems.size(); i++) 25 | memcpy(ret.data() + m_offsets[i], m_view_elems[i].data(), m_view_elems[i].size()); 26 | return ret; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /cpp/test/test_for.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | 5 | int main() 6 | { 7 | TRTC_For f({ "arr_in", "arr_out", "k" }, "idx", 8 | " arr_out[idx] = arr_in[idx]*k;\n"); 9 | 10 | float test_f[5] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0 }; 11 | DVVector dvec_in_f("float", 5, test_f); 12 | DVVector dvec_out_f("float", 5); 13 | DVDouble k1(10.0); 14 | const DeviceViewable* args_f[] = { &dvec_in_f, &dvec_out_f, &k1 }; 15 | f.launch_n(5, args_f); 16 | dvec_out_f.to_host(test_f); 17 | printf("%f %f %f %f %f\n", test_f[0], test_f[1], test_f[2], test_f[3], test_f[4]); 18 | 19 | int test_i[5] = { 6, 7, 8, 9, 10 }; 20 | DVVector dvec_in_i("int32_t", 5, test_i); 21 | DVVector dvec_out_i("int32_t", 5); 22 | DVDouble k2(5.0); 23 | const DeviceViewable* args_i[] = { &dvec_in_i, &dvec_out_i, &k2 }; 24 | f.launch_n(5, args_i); 25 | dvec_out_i.to_host(test_i); 26 | printf("%d %d %d %d %d\n", test_i[0], test_i[1], test_i[2], test_i[3], test_i[4]); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /cpp/test/test_sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "sequence.h" 5 | 6 | int main() 7 | { 8 | int hvalues[10]; 9 | DVVector vec("int32_t", 10); 10 | 11 | TRTC_Sequence(vec); 12 | vec.to_host(hvalues); 13 | printf("%d %d %d %d %d ", hvalues[0], hvalues[1], hvalues[2], hvalues[3], hvalues[4]); 14 | printf("%d %d %d %d %d\n", hvalues[5], hvalues[6], hvalues[7], hvalues[8], hvalues[9]); 15 | 16 | TRTC_Sequence(vec, DVInt32(1)); 17 | vec.to_host(hvalues); 18 | printf("%d %d %d %d %d ", hvalues[0], hvalues[1], hvalues[2], hvalues[3], hvalues[4]); 19 | printf("%d %d %d %d %d\n", hvalues[5], hvalues[6], hvalues[7], hvalues[8], hvalues[9]); 20 | 21 | TRTC_Sequence(vec, DVInt32(1), DVInt32(3)); 22 | vec.to_host(hvalues); 23 | printf("%d %d %d %d %d ", hvalues[0], hvalues[1], hvalues[2], hvalues[3], hvalues[4]); 24 | printf("%d %d %d %d %d\n", hvalues[5], hvalues[6], hvalues[7], hvalues[8], hvalues[9]); 25 | 26 | 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /.github/workflows/conda_ubuntu.yml: -------------------------------------------------------------------------------- 1 | name: Conda Ubuntu Publishment 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | paths: 7 | - 'conda_ubuntu/meta.yaml' 8 | 9 | jobs: 10 | build_ubuntu_latest: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | with: 16 | submodules: true 17 | 18 | - name: Set up Python 3.8 19 | uses: actions/setup-python@v2 20 | with: 21 | python-version: 3.8 22 | 23 | - name: Add conda to system path 24 | run: | 25 | echo $CONDA/bin >> $GITHUB_PATH 26 | 27 | - name: Set Always Yes 28 | run: conda config --set always_yes yes 29 | 30 | - name: Install conda tools 31 | run: conda install conda-build anaconda-client 32 | 33 | - name: Set Auto Upload 34 | run: conda config --set anaconda_upload yes 35 | 36 | - name: conda build and release 37 | run: conda-build conda_ubuntu --no-test --user fyplus --token ${{ secrets.ANACONDA_TOKEN }} 38 | -------------------------------------------------------------------------------- /java/test/test_for.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_for 5 | { 6 | public static void main(String[] args) 7 | { 8 | For ker = new For(new String[] { "arr_in", "arr_out", "k" }, "idx", 9 | " arr_out[idx] = arr_in[idx]*k;\n"); 10 | 11 | DVVector dvec_in_f = new DVVector(new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }); 12 | DVVector dvec_out_f = new DVVector("float", 5); 13 | DVFloat k1 = new DVFloat(10.0f); 14 | DeviceViewable[] args_f = new DeviceViewable[] { dvec_in_f, dvec_out_f, k1 }; 15 | ker.launch_n(5, args_f); 16 | System.out.println(Arrays.toString((float[])dvec_out_f.to_host())); 17 | 18 | DVVector dvec_in_i = new DVVector(new int[] { 6, 7, 8, 9, 10 }); 19 | DVVector dvec_out_i = new DVVector("int32_t", 5); 20 | DVInt32 k2 = new DVInt32(5); 21 | DeviceViewable[] args_i = new DeviceViewable[] { dvec_in_i, dvec_out_i, k2 }; 22 | ker.launch_n(5, args_i); 23 | System.out.println(Arrays.toString((int[])dvec_out_i.to_host())); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/test/test_logical.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_logical 5 | { 6 | public static void main(String[] args) 7 | { 8 | Functor identity= new Functor("Identity"); 9 | DVVector d_A = new DVVector(new byte[] { 1, 1, 0 }); 10 | 11 | System.out.println(TRTC.All_Of(d_A.range(0,2), identity)); 12 | System.out.println(TRTC.All_Of(d_A.range(0,3), identity)); 13 | System.out.println(TRTC.All_Of(d_A.range(0,0), identity)); 14 | 15 | System.out.println(TRTC.Any_Of(d_A.range(0, 2), identity)); 16 | System.out.println(TRTC.Any_Of(d_A.range(0, 3), identity)); 17 | System.out.println(TRTC.Any_Of(d_A.range(2, 3), identity)); 18 | System.out.println(TRTC.Any_Of(d_A.range(0, 0), identity)); 19 | 20 | System.out.println(TRTC.None_Of(d_A.range(0, 2), identity)); 21 | System.out.println(TRTC.None_Of(d_A.range(0, 3), identity)); 22 | System.out.println(TRTC.None_Of(d_A.range(2, 3), identity)); 23 | System.out.println(TRTC.None_Of(d_A.range(0, 0), identity)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /python/ThrustRTC/utils.py: -------------------------------------------------------------------------------- 1 | from .Native import ffi,native 2 | 3 | class StrArray: 4 | def __init__(self, arr): 5 | c_strs = tuple(ffi.from_buffer('char[]', s.encode('utf-8')) for s in arr) 6 | self.m_cptr = native.n_string_array_create(len(c_strs), c_strs) 7 | 8 | def __del__(self): 9 | native.n_string_array_destroy(self.m_cptr) 10 | 11 | class ObjArray: 12 | def __init__(self, arr): 13 | c_ptrs = tuple(obj.m_cptr for obj in arr) 14 | self.m_cptr = native.n_pointer_array_create(len(c_ptrs), c_ptrs) 15 | 16 | def __del__(self): 17 | native.n_pointer_array_destroy(self.m_cptr) 18 | 19 | class Dim3: 20 | def __init__(self, t): 21 | tp = [1,1,1] 22 | if type(t) is tuple: 23 | tp[0:len(t)] = t[:] 24 | else: 25 | tp[0]=t 26 | 27 | self.m_cptr = native.n_dim3_create(tp[0],tp[1],tp[2]) 28 | 29 | def __del__(self): 30 | native.n_dim3_destroy(self.m_cptr) 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /python/ThrustRTC/Copying.py: -------------------------------------------------------------------------------- 1 | from .Native import ffi, native, check_i 2 | 3 | def Gather(vec_map, vec_in, vec_out): 4 | check_i(native.n_gather(vec_map.m_cptr, vec_in.m_cptr, vec_out.m_cptr)) 5 | 6 | def Gather_If(vec_map, vec_stencil, vec_in, vec_out, pred = None): 7 | cptr_pred = ffi.NULL 8 | if pred!=None: 9 | cptr_pred = pred.m_cptr 10 | check_i(native.n_gather_if(vec_map.m_cptr, vec_stencil.m_cptr, vec_in.m_cptr, vec_out.m_cptr, cptr_pred)) 11 | 12 | def Scatter(vec_in, vec_map, vec_out): 13 | check_i(native.n_scatter(vec_in.m_cptr, vec_map.m_cptr, vec_out.m_cptr)) 14 | 15 | def Scatter_If(vec_in, vec_map, vec_stencil, vec_out, pred = None): 16 | cptr_pred = ffi.NULL 17 | if pred!=None: 18 | cptr_pred = pred.m_cptr 19 | check_i(native.n_scatter_if(vec_in.m_cptr, vec_map.m_cptr, vec_stencil.m_cptr, vec_out.m_cptr, cptr_pred)) 20 | 21 | def Copy(vec_in, vec_out): 22 | check_i(native.n_copy(vec_in.m_cptr, vec_out.m_cptr)) 23 | 24 | def Swap(vec1, vec2): 25 | check_i(native.n_swap(vec1.m_cptr, vec2.m_cptr)) 26 | 27 | -------------------------------------------------------------------------------- /count.cpp: -------------------------------------------------------------------------------- 1 | #include "count.h" 2 | #include "general_reduce.h" 3 | #include "built_in.h" 4 | 5 | bool TRTC_Count(const DVVectorLike& vec, const DeviceViewable& value, size_t& ret) 6 | { 7 | Functor src({ {"vec_in", &vec}, {"eq_value", &value } }, { "idx" }, 8 | " return (vec_in[idx] == (decltype(vec_in)::value_t)eq_value)?1:0;\n"); 9 | 10 | Functor op("Plus"); 11 | 12 | ret = 0; 13 | if (vec.size() < 1) return true; 14 | 15 | ViewBuf buf; 16 | if (!general_reduce(vec.size(), "size_t", src, op, buf)) return false; 17 | ret = *(size_t*)buf.data(); 18 | return true; 19 | } 20 | 21 | bool TRTC_Count_If(const DVVectorLike& vec, const Functor& pred, size_t& ret) 22 | { 23 | Functor src({ {"vec_in", &vec}, {"pred", &pred } }, { "idx" }, 24 | " return pred(vec_in[idx])?1:0;\n"); 25 | Functor op("Plus"); 26 | 27 | ret = 0; 28 | if (vec.size() < 1) return true; 29 | 30 | ViewBuf buf; 31 | if (!general_reduce(vec.size(), "size_t", src, op, buf)) return false; 32 | ret = *(size_t*)buf.data(); 33 | return true; 34 | 35 | } -------------------------------------------------------------------------------- /fake_vectors/DVPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DVPermutation.h" 3 | 4 | DVPermutation::DVPermutation(const DVVectorLike& vec_value, const DVVectorLike& vec_index) 5 | : DVVectorLike(vec_value.name_elem_cls().c_str(), vec_value.name_ref_type().c_str(), vec_index.size()) 6 | { 7 | m_readable = vec_value.is_readable(); 8 | m_writable = vec_value.is_writable(); 9 | m_cls_value = vec_value.name_view_cls(); 10 | m_view_value = vec_value.view(); 11 | m_cls_index = vec_index.name_view_cls(); 12 | m_view_index = vec_index.view(); 13 | 14 | m_name_view_cls = std::string("PermutationView<") + m_cls_value + ", " + m_cls_index + ">"; 15 | TRTC_Query_Struct(m_name_view_cls.c_str(), { "_view_vec_value", "_view_vec_index" }, m_offsets); 16 | 17 | } 18 | 19 | ViewBuf DVPermutation::view() const 20 | { 21 | ViewBuf buf(m_offsets[2]); 22 | memcpy(buf.data() + m_offsets[0], m_view_value.data(), m_view_value.size()); 23 | memcpy(buf.data() + m_offsets[1], m_view_index.data(), m_view_index.size()); 24 | return buf; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /java/DeviceViewable.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | import java.lang.ref.Cleaner; 3 | 4 | public class DeviceViewable implements AutoCloseable 5 | { 6 | private static class State implements Runnable 7 | { 8 | public long m_cptr; 9 | 10 | public State(long _cptr) 11 | { 12 | m_cptr = _cptr; 13 | } 14 | 15 | @Override 16 | public void run() 17 | { 18 | Native.dv_destroy(m_cptr); 19 | } 20 | } 21 | 22 | private final State m_state; 23 | private final Cleaner.Cleanable cleanable; 24 | 25 | public long cptr() { return m_state.m_cptr; } 26 | 27 | public String name_view_cls() 28 | { 29 | return Native.dv_name_view_cls(m_state.m_cptr); 30 | } 31 | 32 | public DeviceViewable(long _cptr) 33 | { 34 | m_state = new State(_cptr); 35 | cleanable = TRTC.cleaner.register(this, m_state); 36 | } 37 | 38 | 39 | @Override 40 | public void close() 41 | { 42 | cleanable.clean(); 43 | } 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /java/DVVectorAdaptor.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class DVVectorAdaptor extends DVVectorLike 4 | { 5 | public DVVectorAdaptor(String elem_cls, int size, long native_pointer) 6 | { 7 | super( Native.dvvectoradaptor_create(elem_cls, size, native_pointer) ); 8 | } 9 | 10 | public DVVectorAdaptor(DVVector vec, int begin, int end) 11 | { 12 | super( Native.dvvectoradaptor_create_from_dvvector(vec.cptr(), begin, end) ); 13 | } 14 | 15 | public DVVectorAdaptor(DVVector vec) 16 | { 17 | super( Native.dvvectoradaptor_create_from_dvvector(vec.cptr(), 0, -1) ); 18 | } 19 | 20 | public DVVectorAdaptor(DVVectorAdaptor vec, int begin, int end) 21 | { 22 | super( Native.dvvectoradaptor_create_from_dvvectoradaptor(vec.cptr(), begin, end) ); 23 | } 24 | 25 | public DVVectorAdaptor(DVVectorAdaptor vec) 26 | { 27 | super( Native.dvvectoradaptor_create_from_dvvectoradaptor(vec.cptr(), 0, -1) ); 28 | } 29 | 30 | public long native_pointer() 31 | { 32 | return Native.dvvectoradaptor_native_pointer(cptr()); 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /reduce.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_reduce_h 2 | #define _TRTC_reduce_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | bool THRUST_RTC_API TRTC_Reduce(const DVVectorLike& vec, ViewBuf& ret); 11 | bool THRUST_RTC_API TRTC_Reduce(const DVVectorLike& vec, const DeviceViewable& init, ViewBuf& ret); 12 | bool THRUST_RTC_API TRTC_Reduce(const DVVectorLike& vec, const DeviceViewable& init, const Functor& binary_op, ViewBuf& ret); 13 | 14 | uint32_t THRUST_RTC_API TRTC_Reduce_By_Key(const DVVectorLike& key_in, const DVVectorLike& value_in, DVVectorLike& key_out, DVVectorLike& value_out); 15 | uint32_t THRUST_RTC_API TRTC_Reduce_By_Key(const DVVectorLike& key_in, const DVVectorLike& value_in, DVVectorLike& key_out, DVVectorLike& value_out, const Functor& binary_pred); 16 | uint32_t THRUST_RTC_API TRTC_Reduce_By_Key(const DVVectorLike& key_in, const DVVectorLike& value_in, DVVectorLike& key_out, DVVectorLike& value_out, const Functor& binary_pred, const Functor& binary_op); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /CSharp/test/test_equal/test_equal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_equal 5 | { 6 | class test_equal 7 | { 8 | static void Main(string[] args) 9 | { 10 | { 11 | DVVector darr1 = new DVVector(new int[] { 3, 1, 4, 1, 5, 9, 3 }); 12 | DVVector darr2 = new DVVector(new int[] { 3, 1, 4, 2, 8, 5, 7 }); 13 | DVVector darr3 = new DVVector(new int[] { 3, 1, 4, 1, 5, 9, 3 }); 14 | 15 | Console.WriteLine(TRTC.Equal(darr1, darr2).ToString() + " " +TRTC.Equal(darr1, darr3).ToString()); 16 | } 17 | 18 | { 19 | Functor compare_modulo_two = new Functor( new string[]{ "x", "y" }, " return (x % 2) == (y % 2);\n" ); 20 | DVVector dx = new DVVector(new int[] { 1, 2, 3, 4, 5, 6 }); 21 | DVVector dy = new DVVector(new int[] { 7, 8, 9, 10, 11, 12 }); 22 | Console.WriteLine(TRTC.Equal(dx, dy, compare_modulo_two)); 23 | } 24 | 25 | } 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /fake_vectors/DVRange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DVRange.h" 3 | 4 | DVRange::DVRange(const DVVectorLike& vec_value, size_t begin, size_t end) 5 | : DVVectorLike(vec_value.name_elem_cls().c_str(), vec_value.name_ref_type().c_str(), (end == (size_t)(-1) ? vec_value.size() : end) - begin) 6 | { 7 | if (end == (size_t)(-1)) end = vec_value.size(); 8 | m_readable = vec_value.is_readable(); 9 | m_writable = vec_value.is_writable(); 10 | m_cls_value = vec_value.name_view_cls(); 11 | m_view_value = vec_value.view(); 12 | m_begin = begin; 13 | m_end = end; 14 | 15 | m_name_view_cls = std::string("RangeView<") + m_cls_value + ">"; 16 | TRTC_Query_Struct(m_name_view_cls.c_str(), { "_view_vec_value", "_begin", "_end" }, m_offsets); 17 | } 18 | 19 | 20 | ViewBuf DVRange::view() const 21 | { 22 | ViewBuf buf(m_offsets[3]); 23 | memcpy(buf.data() + m_offsets[0], m_view_value.data(), m_view_value.size()); 24 | memcpy(buf.data() + m_offsets[1], &m_begin, sizeof(size_t)); 25 | memcpy(buf.data() + m_offsets[2], &m_end, sizeof(size_t)); 26 | return buf; 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /cpp/test/test_custom_vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "sequence.h" 5 | #include "reduce.h" 6 | #include "fake_vectors/DVCustomVector.h" 7 | #include "fake_vectors/DVDiscard.h" 8 | 9 | int main() 10 | { 11 | 12 | DVVector din("int32_t", 2000); 13 | TRTC_Sequence(din); 14 | 15 | DVCustomVector d_custom_values({ {"src", &din} }, "idx", 16 | " unsigned group = idx / src.size();\n" 17 | " unsigned sub_idx = idx % src.size();\n" 18 | " return src[sub_idx] % (group+1) ==0 ? 1: 0;\n ", "uint32_t", din.size() * 10); 19 | 20 | DVCustomVector d_custom_keys({ {"src", &din} }, "idx", 21 | " return idx / src.size();\n", "uint32_t", din.size() * 10); 22 | 23 | DVVector d_values_out("uint32_t", 10); 24 | DVDiscard d_keys_out("uint32_t", 10); 25 | 26 | TRTC_Reduce_By_Key(d_custom_keys, d_custom_values, d_keys_out, d_values_out); 27 | 28 | unsigned out[10]; 29 | d_values_out.to_host(out); 30 | for (int i = 0; i < 10; i++) 31 | printf("%d ", out[i]); 32 | puts(""); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /adjacent_difference.cpp: -------------------------------------------------------------------------------- 1 | #include "adjacent_difference.h" 2 | 3 | bool TRTC_Adjacent_Difference(const DVVectorLike& vec_in, DVVectorLike& vec_out) 4 | { 5 | static TRTC_For s_for({ "view_vec_in", "view_vec_out"}, "idx", 6 | " auto value = view_vec_in[idx ];\n" 7 | " if (idx>0) value -= view_vec_in[idx - 1]; \n" 8 | " view_vec_out[idx] = (decltype(view_vec_out)::value_t) value;\n" 9 | ); 10 | 11 | const DeviceViewable* args[] = { &vec_in, &vec_out }; 12 | return s_for.launch_n(vec_in.size(), args); 13 | } 14 | 15 | bool TRTC_Adjacent_Difference(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& binary_op) 16 | { 17 | static TRTC_For s_for({ "view_vec_in", "view_vec_out", "binary_op" }, "idx", 18 | " auto value = view_vec_in[idx];\n" 19 | " if (idx>0) value = (decltype(view_vec_in)::value_t) binary_op(value, view_vec_in[idx - 1]); \n" 20 | " view_vec_out[idx] = (decltype(view_vec_out)::value_t) value;\n" 21 | ); 22 | 23 | const DeviceViewable* args[] = { &vec_in, &vec_out, &binary_op }; 24 | return s_for.launch_n(vec_in.size(), args); 25 | } 26 | -------------------------------------------------------------------------------- /python/test/test_reduce.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | darr = trtc.device_vector_from_list([1, 0, 2, 2, 1, 3], 'int32_t') 6 | print(trtc.Reduce(darr)) 7 | print(trtc.Reduce(darr, trtc.DVInt32(1))) 8 | print(trtc.Reduce(darr, trtc.DVInt32(-1), trtc.Maximum())) 9 | 10 | d_keys_in = trtc.device_vector_from_list([1, 3, 3, 3, 2, 2, 1], 'int32_t') 11 | d_value_in = trtc.device_vector_from_list([9, 8, 7, 6, 5, 4, 3], 'int32_t') 12 | 13 | d_keys_out = trtc.device_vector('int32_t', 7) 14 | d_values_out = trtc.device_vector('int32_t', 7) 15 | 16 | count = trtc.Reduce_By_Key(d_keys_in, d_value_in, d_keys_out, d_values_out) 17 | print (d_keys_out.to_host(0, count)) 18 | print (d_values_out.to_host(0, count)) 19 | 20 | count = trtc.Reduce_By_Key(d_keys_in, d_value_in, d_keys_out, d_values_out, trtc.EqualTo()) 21 | print (d_keys_out.to_host(0, count)) 22 | print (d_values_out.to_host(0, count)) 23 | 24 | count = trtc.Reduce_By_Key(d_keys_in, d_value_in, d_keys_out, d_values_out, trtc.EqualTo(), trtc.Plus()) 25 | print (d_keys_out.to_host(0, count)) 26 | print (d_values_out.to_host(0, count)) 27 | -------------------------------------------------------------------------------- /equal.cpp: -------------------------------------------------------------------------------- 1 | #include "equal.h" 2 | 3 | bool TRTC_Equal(const DVVectorLike& vec1, const DVVectorLike& vec2, bool& ret) 4 | { 5 | static TRTC_For s_for({ "view_vec1", "view_vec2", "view_res" }, "idx", 6 | " if (view_vec1[idx ]!=(decltype(view_vec1)::value_t)view_vec2[idx ]) view_res[0]=false;\n" 7 | ); 8 | 9 | ret = true; 10 | DVVector dvres("bool", 1, &ret); 11 | 12 | const DeviceViewable* args[] = { &vec1, &vec2, &dvres }; 13 | if (!s_for.launch_n(vec1.size(), args)) return false; 14 | dvres.to_host(&ret); 15 | return true; 16 | } 17 | 18 | bool TRTC_Equal(const DVVectorLike& vec1, const DVVectorLike& vec2, const Functor& binary_pred, bool& ret) 19 | { 20 | static TRTC_For s_for({ "view_vec1", "view_vec2", "view_res", "binary_pred" }, "idx", 21 | " if (!binary_pred(view_vec1[idx], view_vec2[idx])) view_res[0]=false;\n" 22 | ); 23 | 24 | ret = true; 25 | DVVector dvres("bool", 1, &ret); 26 | 27 | const DeviceViewable* args[] = { &vec1, &vec2, &dvres, &binary_pred }; 28 | if (!s_for.launch_n(vec1.size(), args)) return false; 29 | dvres.to_host(&ret); 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /python/test/test_zipped.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | d_int_in = trtc.device_vector_from_list([0, 1, 2, 3, 4], 'int32_t') 6 | d_float_in = trtc.device_vector_from_list([ 0.0, 10.0, 20.0, 30.0, 40.0], 'float') 7 | 8 | d_int_out = trtc.device_vector('int32_t', 5) 9 | d_float_out = trtc.device_vector('float', 5) 10 | 11 | zipped_in = trtc.DVZipped([d_int_in, d_float_in], ['a','b']) 12 | zipped_out = trtc.DVZipped([d_int_out, d_float_out], ['a','b']) 13 | 14 | trtc.Copy(zipped_in, zipped_out) 15 | print (d_int_out.to_host()) 16 | print (d_float_out.to_host()) 17 | 18 | d_int_in = trtc.DVCounter(trtc.DVInt32(0), 5) 19 | d_float_in = trtc.DVTransform(d_int_in, "float", trtc.Functor({}, ['i'], ' return (float)i*10.0f +10.0f;\n')) 20 | zipped_in = trtc.DVZipped([d_int_in, d_float_in], ['a','b']) 21 | trtc.Copy(zipped_in, zipped_out) 22 | print (d_int_out.to_host()) 23 | print (d_float_out.to_host()) 24 | 25 | const_in = trtc.DVConstant(trtc.DVTuple({'a': trtc.DVInt32(123), 'b': trtc.DVFloat(456.0)}), 5) 26 | trtc.Copy(const_in, zipped_out) 27 | print (d_int_out.to_host()) 28 | print (d_float_out.to_host()) 29 | -------------------------------------------------------------------------------- /python/ThrustRTC/Native.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import site 4 | from .cffi import ffi 5 | 6 | if os.name == 'nt': 7 | fn_thrustrtc = 'PyThrustRTC.dll' 8 | elif os.name == "posix": 9 | fn_thrustrtc = 'libPyThrustRTC.so' 10 | 11 | path_thrustrtc = os.path.dirname(__file__)+"/"+fn_thrustrtc 12 | native = ffi.dlopen(path_thrustrtc) 13 | 14 | def check_i(ret_value_i): 15 | if ret_value_i == -1: 16 | raise SystemError("An internal error happend") 17 | elif ret_value_i == -2: 18 | raise ValueError("Wrong number of arguments.") 19 | elif ret_value_i == -100: 20 | raise ImportError('cannot import ThrustRTC') 21 | return ret_value_i 22 | 23 | def check_u(ret_value_u): 24 | if ret_value_u == 0xFFFFFFFF: 25 | raise SystemError("An internal error happend") 26 | return ret_value_u 27 | 28 | def check_ull(ret_value_ull): 29 | if ret_value_ull == 0xFFFFFFFFFFFFFFFF: 30 | raise SystemError("An internal error happend") 31 | return ret_value_ull 32 | 33 | def check_cptr(ret_value_cptr): 34 | if ret_value_cptr == ffi.NULL: 35 | raise SystemError("An internal error happend") 36 | return ret_value_cptr 37 | 38 | -------------------------------------------------------------------------------- /unique.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRTC_unique_h 2 | #define _TRTC_unique_h 3 | 4 | #include "TRTC_api.h" 5 | #include "TRTCContext.h" 6 | #include "DeviceViewable.h" 7 | #include "DVVector.h" 8 | #include "functor.h" 9 | 10 | uint32_t THRUST_RTC_API TRTC_Unique(DVVectorLike& vec); 11 | uint32_t THRUST_RTC_API TRTC_Unique(DVVectorLike& vec, const Functor& binary_pred); 12 | uint32_t THRUST_RTC_API TRTC_Unique_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out); 13 | uint32_t THRUST_RTC_API TRTC_Unique_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& binary_pred); 14 | uint32_t THRUST_RTC_API TRTC_Unique_By_Key(DVVectorLike& keys, DVVectorLike& values); 15 | uint32_t THRUST_RTC_API TRTC_Unique_By_Key(DVVectorLike& keys, DVVectorLike& values, const Functor& binary_pred); 16 | uint32_t THRUST_RTC_API TRTC_Unique_By_Key_Copy(const DVVectorLike& keys_in, const DVVectorLike& values_in, DVVectorLike& keys_out, DVVectorLike& values_out); 17 | uint32_t THRUST_RTC_API TRTC_Unique_By_Key_Copy(const DVVectorLike& keys_in, const DVVectorLike& values_in, DVVectorLike& keys_out, DVVectorLike& values_out, const Functor& binary_pred); 18 | 19 | 20 | #endif -------------------------------------------------------------------------------- /CSharp/ThrustRTCSharp/DVTuple.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCLR; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace ThrustRTCSharp 6 | { 7 | public class DVTuple : DeviceViewable 8 | { 9 | private static IntPtr create(DeviceViewable[] objs, string[] name_objs) 10 | { 11 | CapturedDeviceViewable_clr[] arg_map = new CapturedDeviceViewable_clr[objs.Length]; 12 | for (int i = 0; i < objs.Length; i++) 13 | { 14 | arg_map[i].obj = objs[i].m_cptr; 15 | arg_map[i].obj_name = Marshal.StringToHGlobalAnsi(name_objs[i]); 16 | } 17 | 18 | return Native.dvtuple_create(arg_map); 19 | } 20 | 21 | public DVTuple(DeviceViewable[] objs, string[] name_objs) : base(create(objs, name_objs)) 22 | { 23 | m_objs = objs; 24 | } 25 | 26 | protected override void Dispose(bool disposing) 27 | { 28 | if (disposed) return; 29 | m_objs = null; 30 | base.Dispose(disposing); 31 | } 32 | 33 | private DeviceViewable[] m_objs; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /copy.cpp: -------------------------------------------------------------------------------- 1 | #include "copy.h" 2 | 3 | bool TRTC_Copy(const DVVectorLike& vec_in, DVVectorLike& vec_out) 4 | { 5 | static TRTC_For s_for( 6 | { "view_vec_in", "view_vec_out"}, "idx", 7 | " view_vec_out[idx]=(decltype(view_vec_out)::value_t)view_vec_in[idx];\n" 8 | ); 9 | 10 | const DeviceViewable* args[] = { &vec_in, &vec_out }; 11 | return s_for.launch_n(vec_in.size(), args); 12 | } 13 | 14 | #include "general_copy_if.h" 15 | 16 | uint32_t TRTC_Copy_If(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& pred) 17 | { 18 | Functor src_scan({ {"vec_in", &vec_in}, {"pred", &pred}}, { "idx" }, 19 | " return pred(vec_in[idx])? (uint32_t)1:(uint32_t)0;\n"); 20 | return general_copy_if(vec_in.size(), src_scan, vec_in, vec_out); 21 | } 22 | 23 | uint32_t TRTC_Copy_If_Stencil(const DVVectorLike& vec_in, const DVVectorLike& vec_stencil, DVVectorLike& vec_out, const Functor& pred) 24 | { 25 | Functor src_scan({ {"vec_stencil", &vec_stencil}, {"pred", &pred}}, { "idx" }, 26 | " return pred(vec_stencil[idx])? (uint32_t)1:(uint32_t)0;"); 27 | return general_copy_if(vec_in.size(), src_scan, vec_in, vec_out); 28 | } 29 | -------------------------------------------------------------------------------- /java/Functor.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class Functor extends DeviceViewable 4 | { 5 | public Functor(String[] functor_params, String code_body) 6 | { 7 | super(Native.functor_create(functor_params, code_body)); 8 | m_objs = null; 9 | } 10 | 11 | private static long create(DeviceViewable[] objs, String[] name_objs, String[] functor_params, String code_body) 12 | { 13 | long[] p_objs = new long[objs.length]; 14 | for (int i = 0; i(T[] arr) 24 | { 25 | foreach (var item in arr) 26 | { 27 | Console.Write(item.ToString()); 28 | Console.Write(" "); 29 | } 30 | Console.WriteLine(""); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /python/test/test_trtc.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | import numpy as np 3 | 4 | 5 | 6 | # interface with numpy 7 | harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32') 8 | darr = trtc.device_vector_from_numpy(harr) 9 | print(darr.to_host()) 10 | 11 | # C data type 12 | print(darr.name_view_cls()) 13 | 14 | harr2 = np.array([6,7,8,9,10], dtype='int32') 15 | darr2 = trtc.device_vector_from_numpy(harr2) 16 | 17 | # kernel with auto parameters, launched twice with different types 18 | kernel = trtc.Kernel(['arr_in', 'arr_out', 'k'], 19 | ''' 20 | size_t idx = blockIdx.x * blockDim.x + threadIdx.x; 21 | if (idx >= arr_in.size()) return; 22 | arr_out[idx] = arr_in[idx]*k; 23 | ''') 24 | 25 | darr_out = trtc.device_vector('float', 5) 26 | kernel.launch(1,128, [darr, darr_out, trtc.DVFloat(10.0)]) 27 | print (darr_out.to_host()) 28 | 29 | darr_out = trtc.device_vector('int32_t', 5) 30 | kernel.launch(1,128, [darr2, darr_out, trtc.DVInt32(5)]) 31 | print (darr_out.to_host()) 32 | 33 | # create a vector from python list with C type specified 34 | darr3 = trtc.device_vector_from_list([3.0, 5.0, 7.0, 9.0 , 11.0], 'float') 35 | print(darr3.to_host()) 36 | 37 | -------------------------------------------------------------------------------- /java/test/test_scan_by_key.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_scan_by_key 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector d_keys = new DVVector(new int[] { 0, 0, 0, 1, 1, 2, 3, 3, 3, 3 }); 10 | DVVector d_values = new DVVector(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); 11 | TRTC.Inclusive_Scan_By_Key(d_keys, d_values, d_values); 12 | System.out.println(Arrays.toString((int[])d_values.to_host())); 13 | } 14 | 15 | { 16 | DVVector d_keys = new DVVector(new int[] { 0, 0, 0, 1, 1, 2, 3, 3, 3, 3 }); 17 | DVVector d_values = new DVVector(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); 18 | TRTC.Exclusive_Scan_By_Key(d_keys, d_values, d_values); 19 | System.out.println(Arrays.toString((int[])d_values.to_host())); 20 | } 21 | 22 | { 23 | DVVector d_keys = new DVVector(new int[] { 0, 0, 0, 1, 1, 2, 3, 3, 3, 3 }); 24 | DVVector d_values = new DVVector(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); 25 | TRTC.Exclusive_Scan_By_Key(d_keys, d_values, d_values, new DVInt32(5)); 26 | System.out.println(Arrays.toString((int[])d_values.to_host())); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/test/test_trtc.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_trtc 5 | { 6 | public static void main(String[] args) 7 | { 8 | Kernel ker = new Kernel(new String[]{ "arr_in", "arr_out", "k" }, 9 | String.join("", 10 | " size_t idx = blockIdx.x * blockDim.x + threadIdx.x;\n", 11 | " if (idx >= arr_in.size()) return;\n", 12 | " arr_out[idx] = arr_in[idx]*k;\n")); 13 | 14 | DVVector dvec_in_f = new DVVector(new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }); 15 | DVVector dvec_out_f = new DVVector("float", 5); 16 | DVFloat k1 = new DVFloat(10.0f); 17 | DeviceViewable[] args_f = new DeviceViewable[] { dvec_in_f, dvec_out_f, k1 }; 18 | ker.launch(1, 128, args_f, 0); 19 | System.out.println(Arrays.toString((float[])dvec_out_f.to_host())); 20 | 21 | DVVector dvec_in_i = new DVVector(new int[] { 6, 7, 8, 9, 10 }); 22 | DVVector dvec_out_i = new DVVector("int32_t", 5); 23 | DVInt32 k2 = new DVInt32(5); 24 | DeviceViewable[] args_i = new DeviceViewable[] { dvec_in_i, dvec_out_i, k2 }; 25 | ker.launch(1, 128, args_i, 0); 26 | System.out.println(Arrays.toString((int[])dvec_out_i.to_host())); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/Sorting.cpp: -------------------------------------------------------------------------------- 1 | #include "JThrustRTC_Native.h" 2 | #include "sort.h" 3 | 4 | JNIEXPORT jboolean JNICALL Java_JThrustRTC_Native_sort__J(JNIEnv *, jclass, jlong p_vec) 5 | { 6 | DVVectorLike* vec = (DVVectorLike*)(p_vec); 7 | return TRTC_Sort(*vec) ? 1 : 0; 8 | } 9 | 10 | JNIEXPORT jboolean JNICALL Java_JThrustRTC_Native_sort__JJ(JNIEnv *, jclass, jlong p_vec, jlong p_comp) 11 | { 12 | DVVectorLike* vec = (DVVectorLike*)(p_vec); 13 | Functor* comp = (Functor*)(p_comp); 14 | return TRTC_Sort(*vec, *comp) ? 1 : 0; 15 | } 16 | 17 | JNIEXPORT jboolean JNICALL Java_JThrustRTC_Native_sort_1by_1key__JJ(JNIEnv *, jclass, jlong p_keys, jlong p_values) 18 | { 19 | DVVectorLike* keys = (DVVectorLike*)(p_keys); 20 | DVVectorLike* values = (DVVectorLike*)(p_values); 21 | return TRTC_Sort_By_Key(*keys, *values) ? 1 : 0; 22 | } 23 | 24 | JNIEXPORT jboolean JNICALL Java_JThrustRTC_Native_sort_1by_1key__JJJ(JNIEnv *, jclass, jlong p_keys, jlong p_values, jlong p_comp) 25 | { 26 | DVVectorLike* keys = (DVVectorLike*)(p_keys); 27 | DVVectorLike* values = (DVVectorLike*)(p_values); 28 | Functor* comp = (Functor*)(p_comp); 29 | return TRTC_Sort_By_Key(*keys, *values, *comp) ? 1 : 0; 30 | } 31 | -------------------------------------------------------------------------------- /python/api_Functor.cpp: -------------------------------------------------------------------------------- 1 | #include "api.h" 2 | #include "TRTCContext.h" 3 | #include "functor.h" 4 | 5 | typedef std::vector StrArray; 6 | typedef std::vector PtrArray; 7 | 8 | 9 | void* n_functor_create(void* ptr_dvs, void* ptr_names, void* ptr_functor_params, const char* code_body) 10 | { 11 | PtrArray* dvs = (PtrArray*)ptr_dvs; 12 | StrArray* names = (StrArray*)ptr_names; 13 | size_t num_params = dvs->size(); 14 | std::vector arg_map(num_params); 15 | for (size_t i = 0; i < num_params; i++) 16 | { 17 | arg_map[i].obj_name = (*names)[i].c_str(); 18 | arg_map[i].obj = (*dvs)[i]; 19 | } 20 | 21 | StrArray* str_functor_params = (StrArray*)ptr_functor_params; 22 | size_t num_functor_params = str_functor_params->size(); 23 | std::vector functor_params(num_functor_params); 24 | for (size_t i = 0; i < num_functor_params; i++) 25 | functor_params[i] = (*str_functor_params)[i].c_str(); 26 | 27 | return new Functor(arg_map, functor_params, code_body); 28 | } 29 | 30 | void* n_built_in_functor_create(const char* name_built_in_view_cls) 31 | { 32 | return new Functor(name_built_in_view_cls); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/Sorting.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ThrustRTCLR.h" 3 | #include "sort.h" 4 | 5 | namespace ThrustRTCLR 6 | { 7 | template 8 | inline T* just_cast_it(IntPtr p) 9 | { 10 | return (T*)(void*)p; 11 | } 12 | 13 | bool Native::sort(IntPtr p_vec) 14 | { 15 | DVVectorLike* vec = just_cast_it(p_vec); 16 | return TRTC_Sort(*vec); 17 | } 18 | 19 | bool Native::sort(IntPtr p_vec, IntPtr p_comp) 20 | { 21 | DVVectorLike* vec = just_cast_it(p_vec); 22 | Functor* comp = just_cast_it(p_comp); 23 | return TRTC_Sort(*vec, *comp); 24 | } 25 | 26 | bool Native::sort_by_key(IntPtr p_keys, IntPtr p_values) 27 | { 28 | DVVectorLike* keys = just_cast_it(p_keys); 29 | DVVectorLike* values = just_cast_it(p_values); 30 | return TRTC_Sort_By_Key(*keys, *values); 31 | } 32 | 33 | bool Native::sort_by_key(IntPtr p_keys, IntPtr p_values, IntPtr p_comp) 34 | { 35 | DVVectorLike* keys = just_cast_it(p_keys); 36 | DVVectorLike* values = just_cast_it(p_values); 37 | Functor* comp = just_cast_it(p_comp); 38 | return TRTC_Sort_By_Key(*keys, *values, *comp); 39 | } 40 | } -------------------------------------------------------------------------------- /sequence.cpp: -------------------------------------------------------------------------------- 1 | #include "sequence.h" 2 | 3 | bool TRTC_Sequence(DVVectorLike& vec) 4 | { 5 | static TRTC_For s_for( { "view_vec" }, "idx", 6 | " view_vec[idx]= (decltype(view_vec)::value_t)idx;\n" ); 7 | 8 | const DeviceViewable* args[] = { &vec }; 9 | return s_for.launch_n(vec.size(), args); 10 | } 11 | 12 | bool TRTC_Sequence(DVVectorLike& vec, const DeviceViewable& value_init) 13 | { 14 | static TRTC_For s_for( 15 | { "view_vec", "value_init" }, "idx", 16 | " view_vec[idx]= (decltype(view_vec)::value_t)value_init + (decltype(view_vec)::value_t)idx;\n" 17 | ); 18 | 19 | const DeviceViewable* args[] = { &vec, &value_init }; 20 | s_for.launch_n(vec.size(), args); 21 | return true; 22 | } 23 | 24 | bool TRTC_Sequence(DVVectorLike& vec, const DeviceViewable& value_init, const DeviceViewable& value_step) 25 | { 26 | static TRTC_For s_for( 27 | { "view_vec", "value_init", "value_step" }, "idx", 28 | " view_vec[idx]= (decltype(view_vec)::value_t)value_init + (decltype(view_vec)::value_t)idx*(decltype(view_vec)::value_t)value_step;\n" 29 | ); 30 | 31 | const DeviceViewable* args[] = { &vec, &value_init, &value_step }; 32 | s_for.launch_n(vec.size(), args); 33 | return true; 34 | } 35 | -------------------------------------------------------------------------------- /java/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | find_package(Java REQUIRED) 3 | include(UseJava) 4 | 5 | set(JAVA_TESTS 6 | test_trtc.java 7 | test_for.java 8 | test_fill.java 9 | test_replace.java 10 | test_for_each.java 11 | test_adjacent_difference.java 12 | test_sequence.java 13 | test_tabulate.java 14 | test_transform.java 15 | test_gather.java 16 | test_scatter.java 17 | test_swap.java 18 | test_count.java 19 | test_reduce.java 20 | test_equal.java 21 | test_extrema.java 22 | test_inner_product.java 23 | test_transform_reduce.java 24 | test_logical.java 25 | test_scan.java 26 | test_scan_by_key.java 27 | test_transform_scan.java 28 | test_copy.java 29 | test_remove.java 30 | test_unique.java 31 | test_partition.java 32 | test_find.java 33 | test_mismatch.java 34 | test_binary_search.java 35 | test_merge.java 36 | test_sort.java 37 | 38 | test_constant.java 39 | test_counter.java 40 | test_discard.java 41 | test_permutation.java 42 | test_reverse.java 43 | test_transform_iter.java 44 | test_zipped.java 45 | test_custom_vector.java 46 | ) 47 | 48 | add_jar(JThrustRTCTests SOURCES ${JAVA_TESTS} INCLUDE_JARS JThrustRTC) 49 | install_jar(JThrustRTCTests test_java) 50 | 51 | install(FILES run_tests.bat run_tests.sh DESTINATION test_java ) -------------------------------------------------------------------------------- /python/test/test_scatter.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | is_even = trtc.Functor( {}, ['x'], 6 | ''' 7 | return ((x % 2) == 0); 8 | ''') 9 | 10 | dvalues = trtc.device_vector_from_list([1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') 11 | dmap = trtc.device_vector_from_list([ 0, 5, 1, 6, 2, 7, 3, 8, 4, 9], 'int32_t') 12 | doutput = trtc.device_vector('int32_t', 10) 13 | 14 | trtc.Scatter(dvalues, dmap, doutput) 15 | print (doutput.to_host()) 16 | 17 | V = trtc.device_vector_from_list([10, 20, 30, 40, 50, 60, 70, 80], 'int32_t') 18 | M = trtc.device_vector_from_list([ 0, 5, 1, 6, 2, 7, 3, 4], 'int32_t') 19 | S = trtc.device_vector_from_list([ 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') 20 | D = trtc.device_vector_from_list([ 0, 0, 0, 0, 0, 0, 0, 0], 'int32_t') 21 | 22 | trtc.Scatter_If(V, M, S, D) 23 | print (D.to_host()) 24 | 25 | V = trtc.device_vector_from_list([10, 20, 30, 40, 50, 60, 70, 80], 'int32_t') 26 | M = trtc.device_vector_from_list([ 0, 5, 1, 6, 2, 7, 3, 4], 'int32_t') 27 | S = trtc.device_vector_from_list([ 2, 1, 2, 1, 2, 1, 2, 1], 'int32_t') 28 | D = trtc.device_vector_from_list([ 0, 0, 0, 0, 0, 0, 0, 0], 'int32_t') 29 | 30 | trtc.Scatter_If(V, M, S, D, is_even) 31 | print (D.to_host()) 32 | -------------------------------------------------------------------------------- /CSharp/test/test_logical/test_logical.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_logical 5 | { 6 | class test_logical 7 | { 8 | static void Main(string[] args) 9 | { 10 | Functor identity= new Functor("Identity"); 11 | DVVector d_A = new DVVector(new bool[] { true, true, false }); 12 | 13 | Console.WriteLine(TRTC.All_Of(d_A.range(0,2), identity)); 14 | Console.WriteLine(TRTC.All_Of(d_A.range(0,3), identity)); 15 | Console.WriteLine(TRTC.All_Of(d_A.range(0,0), identity)); 16 | 17 | Console.WriteLine(TRTC.Any_Of(d_A.range(0, 2), identity)); 18 | Console.WriteLine(TRTC.Any_Of(d_A.range(0, 3), identity)); 19 | Console.WriteLine(TRTC.Any_Of(d_A.range(2, 3), identity)); 20 | Console.WriteLine(TRTC.Any_Of(d_A.range(0, 0), identity)); 21 | 22 | Console.WriteLine(TRTC.None_Of(d_A.range(0, 2), identity)); 23 | Console.WriteLine(TRTC.None_Of(d_A.range(0, 3), identity)); 24 | Console.WriteLine(TRTC.None_Of(d_A.range(2, 3), identity)); 25 | Console.WriteLine(TRTC.None_Of(d_A.range(0, 0), identity)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /cpp/test/test_find.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "find.h" 5 | 6 | int main() 7 | { 8 | { 9 | int h_values[4] = { 0, 5, 3, 7 }; 10 | DVVector d_values("int32_t", 4, h_values); 11 | size_t res; 12 | TRTC_Find(d_values, DVInt32(3), res); 13 | printf("%d\n", (int)res); 14 | TRTC_Find(d_values, DVInt32(5), res); 15 | printf("%d\n", (int)res); 16 | TRTC_Find(d_values, DVInt32(9), res); 17 | printf("%d\n", (int)res); 18 | } 19 | 20 | { 21 | int h_values[4] = { 0, 5, 3, 7 }; 22 | DVVector d_values("int32_t", 4, h_values); 23 | size_t res; 24 | TRTC_Find_If(d_values, Functor({}, { "x" }, " return x>4;\n"), res); 25 | printf("%d\n", (int)res); 26 | TRTC_Find_If(d_values, Functor({}, { "x" }, " return x>10;\n"), res); 27 | printf("%d\n", (int)res); 28 | } 29 | 30 | { 31 | int h_values[4] = { 0, 5, 3, 7 }; 32 | DVVector d_values("int32_t", 4, h_values); 33 | size_t res; 34 | TRTC_Find_If_Not(d_values, Functor({}, { "x" }, " return x>4;\n"), res); 35 | printf("%d\n", (int)res); 36 | TRTC_Find_If_Not(d_values, Functor({}, { "x" }, " return x>10;\n"), res); 37 | printf("%d\n", (int)res); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /python/api_utils.cpp: -------------------------------------------------------------------------------- 1 | #include "api.h" 2 | #include "TRTCContext.h" 3 | #include "DeviceViewable.h" 4 | #include 5 | #include 6 | #include 7 | 8 | typedef std::vector StrArray; 9 | typedef std::vector PtrArray; 10 | 11 | void* n_string_array_create(unsigned long long size, const char* const* strs) 12 | { 13 | StrArray* ret = new StrArray(size); 14 | for (size_t i = 0; i < size; i++) 15 | (*ret)[i] = strs[i]; 16 | 17 | return ret; 18 | } 19 | 20 | void n_string_array_destroy(void* ptr_arr) 21 | { 22 | StrArray* arr = (StrArray*)ptr_arr; 23 | delete arr; 24 | } 25 | 26 | void* n_pointer_array_create(unsigned long long size, const void* const* ptrs) 27 | { 28 | PtrArray* ret = new PtrArray(size); 29 | memcpy(ret->data(), ptrs, sizeof(void*)*size); 30 | return ret; 31 | } 32 | 33 | void n_pointer_array_destroy(void* ptr_arr) 34 | { 35 | PtrArray* arr = (PtrArray*)ptr_arr; 36 | delete arr; 37 | } 38 | 39 | void* n_dim3_create(unsigned x, unsigned y, unsigned z) 40 | { 41 | dim_type* ret = new dim_type({ x,y,z }); 42 | return ret; 43 | } 44 | 45 | void n_dim3_destroy(void* cptr) 46 | { 47 | dim_type* v = (dim_type*)cptr; 48 | delete v; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /CSharp/test/test_adjacent_difference/test_adjacent_difference.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_adjacent_difference 5 | { 6 | class test_adjacent_difference 7 | { 8 | static void Main(string[] args) 9 | { 10 | { 11 | DVVector vec_in = new DVVector(new int[] { 1, 2, 1, 2, 1, 2, 1, 2 }); 12 | DVVector vec_out = new DVVector("int32_t", 8); 13 | TRTC.Adjacent_Difference(vec_in, vec_out); 14 | print_array((int[])vec_out.to_host()); 15 | } 16 | 17 | { 18 | DVVector vec_in = new DVVector(new int[] { 1, 2, 1, 2, 1, 2, 1, 2 }); 19 | DVVector vec_out = new DVVector("int32_t", 8); 20 | TRTC.Adjacent_Difference(vec_in, vec_out, new Functor("Plus")); 21 | print_array((int[])vec_out.to_host()); 22 | } 23 | } 24 | 25 | static void print_array(T[] arr) 26 | { 27 | foreach (var item in arr) 28 | { 29 | Console.Write(item.ToString()); 30 | Console.Write(" "); 31 | } 32 | Console.WriteLine(""); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /cpp/test/test_trtc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | 5 | int main() 6 | { 7 | // kernel with auto parameters, launched twice with different types 8 | TRTC_Kernel ker( 9 | { "arr_in", "arr_out", "k" }, 10 | " size_t idx = blockIdx.x * blockDim.x + threadIdx.x;\n" 11 | " if (idx >= arr_in.size()) return;\n" 12 | " arr_out[idx] = arr_in[idx]*k;\n"); 13 | 14 | float test_f[5] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0 }; 15 | DVVector dvec_in_f("float", 5, test_f); 16 | DVVector dvec_out_f("float", 5); 17 | DVFloat k1(10.0); 18 | const DeviceViewable* args_f[] = { &dvec_in_f, &dvec_out_f, &k1 }; 19 | ker.launch({ 1, 1, 1 }, { 128, 1, 1 }, args_f); 20 | dvec_out_f.to_host(test_f); 21 | printf("%f %f %f %f %f\n", test_f[0], test_f[1], test_f[2], test_f[3], test_f[4]); 22 | 23 | int test_i[5] = { 6, 7, 8, 9, 10 }; 24 | DVVector dvec_in_i("int32_t", 5, test_i); 25 | DVVector dvec_out_i("int32_t", 5); 26 | DVInt32 k2(5); 27 | const DeviceViewable* args_i[] = { &dvec_in_i, &dvec_out_i, &k2 }; 28 | ker.launch({ 1, 1, 1 }, { 128, 1, 1 }, args_i); 29 | dvec_out_i.to_host(test_i); 30 | printf("%d %d %d %d %d\n", test_i[0], test_i[1], test_i[2], test_i[3], test_i[4]); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /CSharp/test/test_zipped/test_zipped.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_zipped 5 | { 6 | class test_zipped 7 | { 8 | static void Main(string[] args) 9 | { 10 | DVVector d_int_in = new DVVector(new int[] { 0, 1, 2, 3, 4 }); 11 | DVVector d_float_in = new DVVector(new float[] { 0.0f, 10.0f, 20.0f, 30.0f, 40.0f }); 12 | 13 | DVVector d_int_out = new DVVector("int32_t", 5); 14 | DVVector d_float_out = new DVVector("float", 5); 15 | 16 | DVZipped src = new DVZipped(new DVVectorLike[] { d_int_in, d_float_in }, new string[] { "a", "b" }); 17 | DVZipped dst = new DVZipped(new DVVectorLike[] { d_int_out, d_float_out }, new string[] { "a", "b" }); 18 | 19 | TRTC.Copy(src, dst); 20 | 21 | print_array((int[])d_int_out.to_host()); 22 | print_array((float[])d_float_out.to_host()); 23 | } 24 | 25 | static void print_array(T[] arr) 26 | { 27 | foreach (var item in arr) 28 | { 29 | Console.Write(item.ToString()); 30 | Console.Write(" "); 31 | } 32 | Console.WriteLine(""); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /java/test/test_scan.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_scan 5 | { 6 | public static void main(String[] args) 7 | { 8 | { 9 | DVVector darr = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 10 | TRTC.Inclusive_Scan(darr, darr); 11 | System.out.println(Arrays.toString((int[])darr.to_host())); 12 | } 13 | 14 | { 15 | DVVector darr = new DVVector(new int[] { -5, 0, 2, -3, 2, 4, 0, -1, 2, 8 }); 16 | TRTC.Inclusive_Scan(darr, darr, new Functor("Maximum")); 17 | System.out.println(Arrays.toString((int[])darr.to_host())); 18 | } 19 | 20 | { 21 | DVVector darr = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 22 | TRTC.Exclusive_Scan(darr, darr); 23 | System.out.println(Arrays.toString((int[])darr.to_host())); 24 | } 25 | 26 | { 27 | DVVector darr = new DVVector(new int[] { 1, 0, 2, 2, 1, 3 }); 28 | TRTC.Exclusive_Scan(darr, darr, new DVInt32(4)); 29 | System.out.println(Arrays.toString((int[])darr.to_host())); 30 | } 31 | 32 | { 33 | DVVector darr = new DVVector(new int[] { -5, 0, 2, -3, 2, 4, 0, -1, 2, 8 }); 34 | TRTC.Exclusive_Scan(darr, darr, new DVInt32(1), new Functor("Maximum")); 35 | System.out.println(Arrays.toString((int[])darr.to_host())); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cpp/test/test_logical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "logical.h" 5 | 6 | int main() 7 | { 8 | Functor identity("Identity"); 9 | 10 | bool A[3] = { true, true, false }; 11 | DVVector d_A("bool", 3, A); 12 | 13 | bool res; 14 | 15 | TRTC_All_Of(DVVectorAdaptor(d_A, 0, 2), identity, res); 16 | printf("%d\n", res); 17 | TRTC_All_Of(DVVectorAdaptor(d_A, 0, 3), identity, res); 18 | printf("%d\n", res); 19 | TRTC_All_Of(DVVectorAdaptor(d_A, 0, 0), identity, res); 20 | printf("%d\n", res); 21 | 22 | TRTC_Any_Of(DVVectorAdaptor(d_A, 0, 2), identity, res); 23 | printf("%d\n", res); 24 | TRTC_Any_Of(DVVectorAdaptor(d_A, 0, 3), identity, res); 25 | printf("%d\n", res); 26 | TRTC_Any_Of(DVVectorAdaptor(d_A, 2, 3), identity, res); 27 | printf("%d\n", res); 28 | TRTC_Any_Of(DVVectorAdaptor(d_A, 0, 0), identity, res); 29 | printf("%d\n", res); 30 | 31 | TRTC_None_Of(DVVectorAdaptor(d_A, 0, 2), identity, res); 32 | printf("%d\n", res); 33 | TRTC_None_Of(DVVectorAdaptor(d_A, 0, 3), identity, res); 34 | printf("%d\n", res); 35 | TRTC_None_Of(DVVectorAdaptor(d_A, 2, 3), identity, res); 36 | printf("%d\n", res); 37 | TRTC_None_Of(DVVectorAdaptor(d_A, 0, 0), identity, res); 38 | printf("%d\n", res); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /CSharp/ThrustRTCSharp/Merging.cs: -------------------------------------------------------------------------------- 1 | using ThrustRTCLR; 2 | 3 | namespace ThrustRTCSharp 4 | { 5 | public partial class TRTC 6 | { 7 | public static bool Merge(DVVectorLike vec1, DVVectorLike vec2, DVVectorLike vec_out) 8 | { 9 | return Native.merge(vec1.m_cptr, vec2.m_cptr, vec_out.m_cptr); 10 | } 11 | 12 | public static bool Merge(DVVectorLike vec1, DVVectorLike vec2, DVVectorLike vec_out, Functor comp) 13 | { 14 | return Native.merge(vec1.m_cptr, vec2.m_cptr, vec_out.m_cptr, comp.m_cptr); 15 | } 16 | 17 | public static bool Merge_By_Key(DVVectorLike keys1, DVVectorLike keys2, DVVectorLike value1, DVVectorLike value2, DVVectorLike keys_out, DVVectorLike value_out) 18 | { 19 | return Native.merge_by_key(keys1.m_cptr, keys2.m_cptr, value1.m_cptr, value2.m_cptr, keys_out.m_cptr, value_out.m_cptr); 20 | } 21 | 22 | public static bool Merge_By_Key(DVVectorLike keys1, DVVectorLike keys2, DVVectorLike value1, DVVectorLike value2, DVVectorLike keys_out, DVVectorLike value_out, Functor comp) 23 | { 24 | return Native.merge_by_key(keys1.m_cptr, keys2.m_cptr, value1.m_cptr, value2.m_cptr, keys_out.m_cptr, value_out.m_cptr, comp.m_cptr); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java/test/test_replace.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import JThrustRTC.*; 3 | 4 | public class test_replace 5 | { 6 | public static void main(String[] args) 7 | { 8 | Functor is_less_than_zero = new Functor(new String[]{ "x" }, " return x<0;\n" ); 9 | 10 | { 11 | DVVector vec = new DVVector(new int[] { 1, 2, 3, 1, 2 }); 12 | TRTC.Replace(vec, new DVInt32(1), new DVInt32(99)); 13 | System.out.println(Arrays.toString((int[])vec.to_host())); 14 | } 15 | 16 | { 17 | DVVector vec = new DVVector(new int[] { 1, -2, 3, -4, 5 }); 18 | TRTC.Replace_If(vec, is_less_than_zero, new DVInt32(99)); 19 | System.out.println(Arrays.toString((int[])vec.to_host())); 20 | } 21 | 22 | { 23 | DVVector vec_in = new DVVector(new int[] { 1, 2, 3, 1, 2 }); 24 | DVVector vec_out = new DVVector("int32_t", 5); 25 | TRTC.Replace_Copy(vec_in, vec_out, new DVInt32(1), new DVInt32(99)); 26 | System.out.println(Arrays.toString((int[])vec_out.to_host())); 27 | } 28 | 29 | { 30 | DVVector vec_in = new DVVector(new int[] { 1, -2, 3, -4, 5 }); 31 | DVVector vec_out = new DVVector("int32_t", 5); 32 | TRTC.Replace_Copy_If(vec_in, vec_out, is_less_than_zero, new DVInt32(99)); 33 | System.out.println(Arrays.toString((int[])vec_out.to_host())); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /python/test/test_sort.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | dvalues = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 6 | print (trtc.Is_Sorted(dvalues)) 7 | trtc.Sort(dvalues) 8 | print (dvalues.to_host()) 9 | print (trtc.Is_Sorted(dvalues)) 10 | 11 | dvalues = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 12 | print (trtc.Is_Sorted(dvalues, trtc.Greater())) 13 | trtc.Sort(dvalues, trtc.Greater()) 14 | print (dvalues.to_host()) 15 | print (trtc.Is_Sorted(dvalues, trtc.Greater())) 16 | 17 | dkeys = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 18 | dvalues = trtc.device_vector_from_list([ 1, 2, 3, 4, 5, 6], 'int32_t') 19 | trtc.Sort_By_Key(dkeys, dvalues) 20 | print (dkeys.to_host()) 21 | print (dvalues.to_host()) 22 | 23 | dkeys = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 24 | dvalues = trtc.device_vector_from_list([ 1, 2, 3, 4, 5, 6], 'int32_t') 25 | trtc.Sort_By_Key(dkeys, dvalues, trtc.Greater()) 26 | print (dkeys.to_host()) 27 | print (dvalues.to_host()) 28 | 29 | dvalues = trtc.device_vector_from_list([ 0, 1, 2, 3, 0, 1, 2, 3 ], 'int32_t') 30 | print (trtc.Is_Sorted_Until(dvalues)) 31 | 32 | dvalues = trtc.device_vector_from_list([ 3, 2, 1, 0, 3, 2, 1, 0 ], 'int32_t') 33 | print (trtc.Is_Sorted_Until(dvalues, trtc.Greater())) 34 | -------------------------------------------------------------------------------- /inner_product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "inner_product.h" 3 | #include "general_reduce.h" 4 | 5 | bool TRTC_Inner_Product(const DVVectorLike& vec1, const DVVectorLike& vec2, const DeviceViewable& init, ViewBuf& ret) 6 | { 7 | Functor src({ {"vec1", &vec1}, {"vec2", &vec2}, {"init", &init} }, { "idx" }, 8 | " return idx>0 ? vec1[idx - 1] * vec2[idx - 1] : init;\n"); 9 | Functor op("Plus"); 10 | 11 | size_t ret_size = TRTC_Size_Of(init.name_view_cls().c_str()); 12 | ret.resize(ret_size); 13 | memset(ret.data(), 0, ret_size); 14 | if (!general_reduce(vec1.size()+1, init.name_view_cls().c_str(), src, op, ret)) return false; 15 | return true; 16 | } 17 | 18 | bool TRTC_Inner_Product(const DVVectorLike& vec1, const DVVectorLike& vec2, const DeviceViewable& init, ViewBuf& ret, const Functor& binary_op1, const Functor& binary_op2) 19 | { 20 | Functor src({ {"vec1", &vec1}, {"vec2", &vec2}, {"init", &init}, {"binary_op2", &binary_op2} }, { "idx" }, 21 | " return idx>0 ? binary_op2(vec1[idx - 1], vec2[idx - 1]) : init;\n"); 22 | 23 | size_t ret_size = TRTC_Size_Of(init.name_view_cls().c_str()); 24 | ret.resize(ret_size); 25 | memset(ret.data(), 0, ret_size); 26 | if (!general_reduce(vec1.size()+1, init.name_view_cls().c_str(), src, binary_op1, ret)) return false; 27 | return true; 28 | 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /python/test/test_gather.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | is_even = trtc.Functor( {}, ['x'], 6 | ''' 7 | return ((x % 2) == 0); 8 | ''') 9 | 10 | dvalues = trtc.device_vector_from_list([1, 0, 1, 0, 1, 0, 1, 0, 1, 0], 'int32_t') 11 | dmap = trtc.device_vector_from_list([ 0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') 12 | doutput = trtc.device_vector('int32_t', 10) 13 | 14 | trtc.Gather(dmap, dvalues, doutput) 15 | print (doutput.to_host()) 16 | 17 | dvalues = trtc.device_vector_from_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], 'int32_t') 18 | dstencil = trtc.device_vector_from_list([ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ], 'int32_t') 19 | dmap = trtc.device_vector_from_list([ 0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') 20 | doutput = trtc.device_vector_from_list([ 7,7,7,7,7,7,7,7,7,7], 'int32_t') 21 | 22 | trtc.Gather_If(dmap, dstencil, dvalues, doutput) 23 | print (doutput.to_host()) 24 | 25 | 26 | dvalues = trtc.device_vector_from_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], 'int32_t') 27 | dstencil = trtc.device_vector_from_list([ 0, 3, 4, 1, 4, 1, 2, 7, 8, 9 ], 'int32_t') 28 | dmap = trtc.device_vector_from_list([ 0, 2, 4, 6, 8, 1, 3, 5, 7, 9], 'int32_t') 29 | doutput = trtc.device_vector_from_list([ 7,7,7,7,7,7,7,7,7,7], 'int32_t') 30 | 31 | trtc.Gather_If(dmap, dstencil, dvalues, doutput, is_even) 32 | print (doutput.to_host()) 33 | 34 | 35 | -------------------------------------------------------------------------------- /CSharp/ThrustRTCLR/FunctorCLR.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ThrustRTCLR.h" 3 | #include "Functor.h" 4 | 5 | namespace ThrustRTCLR 6 | { 7 | template 8 | inline T* just_cast_it(IntPtr p) 9 | { 10 | return (T*)(void*)p; 11 | } 12 | 13 | IntPtr Native::functor_create(array^ p_arg_map, array^ p_functor_params, IntPtr p_code_body) 14 | { 15 | int num_params = p_arg_map->Length; 16 | std::vector arg_map(num_params); 17 | for (int i = 0; i < num_params; i++) 18 | { 19 | arg_map[i].obj_name = just_cast_it(p_arg_map[i].obj_name); 20 | arg_map[i].obj = just_cast_it(p_arg_map[i].obj); 21 | } 22 | int num_functor_params = p_functor_params->Length; 23 | std::vector functor_params(num_functor_params); 24 | for (int i = 0; i < num_functor_params; i++) 25 | functor_params[i] = just_cast_it(p_functor_params[i]); 26 | const char* code_body = just_cast_it(p_code_body); 27 | return (IntPtr)(new Functor(arg_map, functor_params, code_body)); 28 | } 29 | 30 | IntPtr Native::built_in_functor_create(IntPtr p_name_built_in_view_cls) 31 | { 32 | const char* name_built_in_view_cls = just_cast_it(p_name_built_in_view_cls); 33 | return (IntPtr)(new Functor(name_built_in_view_cls)); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /java/For.java: -------------------------------------------------------------------------------- 1 | package JThrustRTC; 2 | 3 | public class For 4 | { 5 | private static class State implements Runnable 6 | { 7 | public long m_cptr; 8 | 9 | public State(long _cptr) 10 | { 11 | m_cptr = _cptr; 12 | } 13 | 14 | @Override 15 | public void run() 16 | { 17 | Native.for_destroy(m_cptr); 18 | } 19 | } 20 | 21 | private final State m_state; 22 | 23 | public long cptr() { return m_state.m_cptr; } 24 | 25 | public For(String[] param_names, String name_iter, String body) 26 | { 27 | long _cptr = Native.for_create(param_names, name_iter, body); 28 | m_state = new State(_cptr); 29 | TRTC.cleaner.register(this, m_state); 30 | } 31 | 32 | public int num_params() 33 | { 34 | return Native.for_num_params(cptr()); 35 | } 36 | 37 | public boolean launch(int begin, int end, DeviceViewable[] args) 38 | { 39 | long[] p_args = new long[args.length]; 40 | for (int i = 0; i 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "copy.h" 5 | 6 | int main() 7 | { 8 | 9 | { 10 | int hIn[8] = { 10, 20, 30, 40, 50, 60, 70, 80 }; 11 | DVVector dIn("int32_t", 8, hIn); 12 | 13 | int hOut[8]; 14 | DVVector dOut("int32_t", 8); 15 | TRTC_Copy(dIn, dOut); 16 | dOut.to_host(hOut); 17 | 18 | printf("%d %d %d %d ", hOut[0], hOut[1], hOut[2], hOut[3]); 19 | printf("%d %d %d %d\n", hOut[4], hOut[5], hOut[6], hOut[7]); 20 | } 21 | 22 | Functor is_even = { {},{ "x" }, " return x % 2 == 0;\n" }; 23 | 24 | { 25 | int hIn[6] = { -2, 0, -1, 0, 1, 2 }; 26 | DVVector dIn("int32_t", 6, hIn); 27 | int hOut[6]; 28 | DVVector dOut("int32_t", 6); 29 | uint32_t count = TRTC_Copy_If(dIn, dOut, is_even); 30 | dOut.to_host(hOut, 0, count); 31 | for (uint32_t i = 0; i < count; i++) 32 | printf("%d ", hOut[i]); 33 | puts(""); 34 | } 35 | 36 | { 37 | int hIn[6] = { 0, 1, 2, 3, 4, 5 }; 38 | DVVector dIn("int32_t", 6, hIn); 39 | int hStencil[6] = { -2, 0, -1, 0, 1, 2 }; 40 | DVVector dStencil("int32_t", 6, hStencil); 41 | 42 | int hOut[6]; 43 | DVVector dOut("int32_t", 6); 44 | uint32_t count = TRTC_Copy_If_Stencil(dIn, dStencil, dOut, is_even); 45 | dOut.to_host(hOut, 0, count); 46 | for (uint32_t i = 0; i < count; i++) 47 | printf("%d ", hOut[i]); 48 | puts(""); 49 | } 50 | 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /scan.cpp: -------------------------------------------------------------------------------- 1 | #include "scan.h" 2 | #include "general_scan.h" 3 | 4 | bool TRTC_Inclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const Functor& binary_op) 5 | { 6 | Functor src({ {"vec_in", &vec_in} }, { "idx" }, 7 | " return vec_in[idx];\n"); 8 | return general_scan(vec_in.size(), src, vec_out, binary_op); 9 | } 10 | 11 | 12 | bool TRTC_Inclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out) 13 | { 14 | Functor plus("Plus"); 15 | return TRTC_Inclusive_Scan(vec_in, vec_out, plus); 16 | } 17 | 18 | bool TRTC_Exclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out) 19 | { 20 | Functor src({ {"vec_in", &vec_in} }, { "idx" }, 21 | " return idx>0? vec_in[idx - 1] : (decltype(vec_in)::value_t) 0;\n"); 22 | Functor plus("Plus"); 23 | return general_scan(vec_in.size(), src, vec_out, plus); 24 | } 25 | 26 | 27 | bool TRTC_Exclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const DeviceViewable& init, const Functor& binary_op) 28 | { 29 | Functor src({ {"vec_in", &vec_in}, {"init", &init} }, { "idx" }, 30 | " return idx>0? vec_in[idx - 1] : (decltype(vec_in)::value_t)init;\n"); 31 | return general_scan(vec_in.size(), src, vec_out, binary_op); 32 | } 33 | 34 | 35 | bool TRTC_Exclusive_Scan(const DVVectorLike& vec_in, DVVectorLike& vec_out, const DeviceViewable& init) 36 | { 37 | Functor plus("Plus"); 38 | return TRTC_Exclusive_Scan(vec_in, vec_out, init, plus); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /cpp/test/test_replace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TRTCContext.h" 3 | #include "DVVector.h" 4 | #include "replace.h" 5 | 6 | int main() 7 | { 8 | Functor is_less_than_zero = { {}, { "x" }, " return x<0;\n" }; 9 | 10 | // replace 11 | int hvec1[5] = { 1,2,3,1,2 }; 12 | DVVector vec1("int32_t", 5, hvec1); 13 | TRTC_Replace(vec1, DVInt32(1), DVInt32(99)); 14 | vec1.to_host(hvec1); 15 | printf("%d %d %d %d %d\n", hvec1[0], hvec1[1], hvec1[2], hvec1[3], hvec1[4]); 16 | 17 | // replace_if 18 | int hvec2[5] = { 1, -2, 3, -4, 5 }; 19 | DVVector vec2("int32_t", 5, hvec2); 20 | TRTC_Replace_If(vec2, is_less_than_zero, DVInt32(0)); 21 | vec2.to_host(hvec2); 22 | printf("%d %d %d %d %d\n", hvec2[0], hvec2[1], hvec2[2], hvec2[3], hvec2[4]); 23 | 24 | // replace_copy 25 | int hvec3[5] = { 1, 2, 3, 1, 2 }; 26 | DVVector vec3_in("int32_t", 5, hvec3); 27 | DVVector vec3_out("int32_t", 5); 28 | TRTC_Replace_Copy(vec3_in, vec3_out, DVInt32(1), DVInt32(99)); 29 | vec3_out.to_host(hvec3); 30 | printf("%d %d %d %d %d\n", hvec3[0], hvec3[1], hvec3[2], hvec3[3], hvec3[4]); 31 | 32 | // replace_copy_if 33 | int hvec4[5] = { 1, -2, 3, -4, 5 }; 34 | DVVector vec4_in("int32_t", 5, hvec4); 35 | DVVector vec4_out("int32_t", 5); 36 | TRTC_Replace_Copy_If(vec4_in, vec4_out, is_less_than_zero, DVInt32(0)); 37 | vec4_out.to_host(hvec4); 38 | printf("%d %d %d %d %d\n", hvec4[0], hvec4[1], hvec4[2], hvec4[3], hvec4[4]); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /CSharp/test/test_for/test_for.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_for 5 | { 6 | class test_for 7 | { 8 | static void Main(string[] args) 9 | { 10 | For ker = new For(new string[] { "arr_in", "arr_out", "k" }, "idx", 11 | " arr_out[idx] = arr_in[idx]*k;\n"); 12 | 13 | DVVector dvec_in_f = new DVVector(new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }); 14 | DVVector dvec_out_f = new DVVector("float", 5); 15 | DVFloat k1 = new DVFloat(10.0f); 16 | DeviceViewable[] args_f = new DeviceViewable[] { dvec_in_f, dvec_out_f, k1 }; 17 | ker.launch_n(5, args_f); 18 | print_array((float[])dvec_out_f.to_host()); 19 | 20 | DVVector dvec_in_i = new DVVector(new int[] { 6, 7, 8, 9, 10 }); 21 | DVVector dvec_out_i = new DVVector("int32_t", 5); 22 | DVInt32 k2 = new DVInt32(5); 23 | DeviceViewable[] args_i = new DeviceViewable[] { dvec_in_i, dvec_out_i, k2 }; 24 | ker.launch_n(5, args_i); 25 | print_array((int[])dvec_out_i.to_host()); 26 | 27 | } 28 | 29 | static void print_array(T[] arr) 30 | { 31 | foreach (var item in arr) 32 | { 33 | Console.Write(item.ToString()); 34 | Console.Write(" "); 35 | } 36 | Console.WriteLine(""); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /python/test/test_remove.py: -------------------------------------------------------------------------------- 1 | import ThrustRTC as trtc 2 | 3 | 4 | 5 | d_value = trtc.device_vector_from_list([ 3, 1, 4, 1, 5, 9 ], 'int32_t') 6 | count = trtc.Remove(d_value, trtc.DVInt32(1)) 7 | print (d_value.to_host(0, count)) 8 | 9 | d_in = trtc.device_vector_from_list([ -2, 0, -1, 0, 1, 2 ], 'int32_t') 10 | d_out = trtc.device_vector('int32_t', 6) 11 | count = trtc.Remove_Copy(d_in, d_out, trtc.DVInt32(0)) 12 | print (d_out.to_host(0, count)) 13 | 14 | is_even = trtc.Functor( {}, ['x'], 15 | ''' 16 | return x % 2 == 0; 17 | ''') 18 | 19 | d_value = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 20 | count = trtc.Remove_If(d_value, is_even) 21 | print (d_value.to_host(0, count)) 22 | 23 | d_in = trtc.device_vector_from_list([ -2, 0, -1, 0, 1, 2 ], 'int32_t') 24 | d_out = trtc.device_vector('int32_t', 6) 25 | count = trtc.Remove_Copy_If(d_in, d_out, is_even) 26 | print (d_out.to_host(0, count)) 27 | 28 | d_value = trtc.device_vector_from_list([ 1, 4, 2, 8, 5, 7 ], 'int32_t') 29 | d_stencil = trtc.device_vector_from_list([ 0, 1, 1, 1, 0, 0 ], 'int32_t') 30 | count = trtc.Remove_If_Stencil(d_value, d_stencil, trtc.Identity()) 31 | print (d_value.to_host(0, count)) 32 | 33 | d_in = trtc.device_vector_from_list([ -2, 0, -1, 0, 1, 2 ], 'int32_t') 34 | d_stencil = trtc.device_vector_from_list([ 1, 1, 0, 1, 0, 1 ], 'int32_t') 35 | d_out = trtc.device_vector('int32_t', 6) 36 | count = trtc.Remove_Copy_If_Stencil(d_in, d_stencil, d_out, trtc.Identity()) 37 | print (d_out.to_host(0, count)) 38 | -------------------------------------------------------------------------------- /internal/Timing.h: -------------------------------------------------------------------------------- 1 | #ifndef _Timing_h 2 | #define _Timing_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifdef _WIN32 9 | 10 | #define WINDOWS_LEAN_AND_MEAN 11 | #define VC_EXTRALEAN 12 | #define NOMINMAX 13 | #include // QueryPerformanceFrequency, QueryPerformanceCounter 14 | 15 | inline double GetTime() 16 | { 17 | unsigned long long counter, frequency; 18 | QueryPerformanceCounter((LARGE_INTEGER*)(&counter)); 19 | QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); 20 | 21 | return (double)counter / (double)frequency; 22 | } 23 | 24 | #else 25 | # include 26 | # include 27 | 28 | inline double GetTime() 29 | { 30 | timeval tv; 31 | gettimeofday( &tv, NULL ); 32 | return (double)(tv.tv_sec*1000000+tv.tv_usec)/1000000.0; 33 | } 34 | 35 | 36 | #endif 37 | 38 | struct EventItem 39 | { 40 | const char* mark; 41 | double time; 42 | }; 43 | 44 | inline std::list& s_event_list() 45 | { 46 | static std::list _event_list; 47 | return _event_list; 48 | } 49 | 50 | inline void s_put_event(const char* mark) 51 | { 52 | std::list& event_list = s_event_list(); 53 | event_list.push_back({ mark, GetTime() }); 54 | } 55 | 56 | inline void s_print_events() 57 | { 58 | std::list& event_list = s_event_list(); 59 | std::list::iterator it = event_list.begin(); 60 | while (it != event_list.end()) 61 | { 62 | printf("%s: %f\n", it->mark, it->time); 63 | it++; 64 | } 65 | event_list.clear(); 66 | } 67 | 68 | #endif -------------------------------------------------------------------------------- /logical.cpp: -------------------------------------------------------------------------------- 1 | #include "logical.h" 2 | 3 | bool TRTC_All_Of(const DVVectorLike& vec, const Functor& pred, bool& ret) 4 | { 5 | static TRTC_For s_for({ "view_vec", "view_res", "pred" }, "idx", 6 | " if (!pred(view_vec[idx])) view_res[0]=false;\n" 7 | ); 8 | ret = false; 9 | if (vec.size() < 1) return true; 10 | ret = true; 11 | DVVector dvres("bool", 1, &ret); 12 | const DeviceViewable* args[] = { &vec, &dvres, &pred }; 13 | if (!s_for.launch_n(vec.size(), args)) return false; 14 | dvres.to_host(&ret); 15 | return true; 16 | } 17 | 18 | bool TRTC_Any_Of(const DVVectorLike& vec, const Functor& pred, bool& ret) 19 | { 20 | static TRTC_For s_for({ "view_vec", "view_res", "pred" }, "idx", 21 | " if (pred(view_vec[idx])) view_res[0]=true;\n" 22 | ); 23 | 24 | ret = false; 25 | if (vec.size() < 1) return true; 26 | 27 | DVVector dvres("bool", 1, &ret); 28 | const DeviceViewable* args[] = { &vec, &dvres, &pred }; 29 | if (!s_for.launch_n(vec.size(), args)) return false; 30 | dvres.to_host(&ret); 31 | return true; 32 | } 33 | 34 | 35 | bool TRTC_None_Of(const DVVectorLike& vec, const Functor& pred, bool& ret) 36 | { 37 | static TRTC_For s_for({ "view_vec", "view_res", "pred" }, "idx", 38 | " if (pred(view_vec[idx])) view_res[0]=false;\n" 39 | ); 40 | 41 | ret = true; 42 | if (vec.size() < 1) return true; 43 | 44 | DVVector dvres("bool", 1, &ret); 45 | const DeviceViewable* args[] = { &vec, &dvres, &pred }; 46 | if (!s_for.launch_n(vec.size(), args)) return false; 47 | dvres.to_host(&ret); 48 | return true; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /python/api_Merging.cpp: -------------------------------------------------------------------------------- 1 | #include "api.h" 2 | #include "TRTCContext.h" 3 | #include "merge.h" 4 | 5 | int n_merge(void* ptr_vec1, void* ptr_vec2, void* ptr_vec_out, void* ptr_comp) 6 | { 7 | DVVectorLike* vec1 = (DVVectorLike*)ptr_vec1; 8 | DVVectorLike* vec2 = (DVVectorLike*)ptr_vec2; 9 | DVVectorLike* vec_out = (DVVectorLike*)ptr_vec_out; 10 | Functor* comp = (Functor*)ptr_comp; 11 | 12 | if (comp == nullptr) 13 | { 14 | if (TRTC_Merge(*vec1, *vec2, *vec_out)) 15 | return 0; 16 | else 17 | return -1; 18 | } 19 | else 20 | { 21 | if (TRTC_Merge(*vec1, *vec2, *vec_out, *comp)) 22 | return 0; 23 | else 24 | return -1; 25 | } 26 | } 27 | 28 | int n_merge_by_key(void* ptr_keys1, void* ptr_keys2, void* ptr_value1, void* ptr_value2, void* ptr_keys_out, void* ptr_value_out, void* ptr_comp) 29 | { 30 | 31 | DVVectorLike* keys1 = (DVVectorLike*)ptr_keys1; 32 | DVVectorLike* keys2 = (DVVectorLike*)ptr_keys2; 33 | DVVectorLike* value1 = (DVVectorLike*)ptr_value1; 34 | DVVectorLike* value2 = (DVVectorLike*)ptr_value2; 35 | DVVectorLike* keys_out = (DVVectorLike*)ptr_keys_out; 36 | DVVectorLike* value_out = (DVVectorLike*)ptr_value_out; 37 | Functor* comp = (Functor*)ptr_comp; 38 | 39 | if (comp == nullptr) 40 | { 41 | if (TRTC_Merge_By_Key(*keys1, *keys2, *value1, *value2, *keys_out, *value_out)) 42 | return 0; 43 | else 44 | return -1; 45 | } 46 | else 47 | { 48 | if (TRTC_Merge_By_Key(*keys1, *keys2, *value1, *value2, *keys_out, *value_out, *comp)) 49 | return 0; 50 | else 51 | return -1; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /CSharp/test/test_trtc/test_trtc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ThrustRTCSharp; 3 | 4 | namespace test_trtc 5 | { 6 | class test_trtc 7 | { 8 | static void Main(string[] args) 9 | { 10 | Kernel ker = new Kernel(new string[]{ "arr_in", "arr_out", "k" }, 11 | @" 12 | size_t idx = blockIdx.x * blockDim.x + threadIdx.x; 13 | if (idx >= arr_in.size()) return; 14 | arr_out[idx] = arr_in[idx]*k;"); 15 | 16 | DVVector dvec_in_f = new DVVector(new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }); 17 | DVVector dvec_out_f = new DVVector("float", 5); 18 | DVFloat k1 = new DVFloat(10.0f); 19 | DeviceViewable[] args_f = new DeviceViewable[] { dvec_in_f, dvec_out_f, k1 }; 20 | ker.launch(1, 128, args_f); 21 | print_array((float[])dvec_out_f.to_host()); 22 | 23 | DVVector dvec_in_i = new DVVector(new int[] { 6, 7, 8, 9, 10 }); 24 | DVVector dvec_out_i = new DVVector("int32_t", 5); 25 | DVInt32 k2 = new DVInt32(5); 26 | DeviceViewable[] args_i = new DeviceViewable[] { dvec_in_i, dvec_out_i, k2 }; 27 | ker.launch(1, 128, args_i); 28 | print_array((int[])dvec_out_i.to_host()); 29 | 30 | } 31 | 32 | static void print_array(T[] arr) 33 | { 34 | foreach (var item in arr) 35 | { 36 | Console.Write(item.ToString()); 37 | Console.Write(" "); 38 | } 39 | Console.WriteLine(""); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CSharp/test/test_copy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("test_copy")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("test_copy")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2019")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("cafd72f9-14f9-432e-864f-e4f4b57055bf")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CSharp/test/test_fill/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("test_fill")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("test_fill")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2019")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("6f59b988-3bda-442d-ab62-cfd893ecadad")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | --------------------------------------------------------------------------------