<commit_before>! Copyright (C) 2010 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data alien.strings arrays
assocs byte-arrays classes.struct combinators cuda
cuda.contexts cuda.ffi cuda.libraries fry io io.encodings.utf8
kernel locals math math.order math.parser namespaces
prettyprint sequences ;
IN: cuda.devices

: #cuda-devices ( -- n )
    { int } [ cuDeviceGetCount cuda-error ] with-out-parameters ;

: n>cuda-device ( n -- device )
    [ { CUdevice } ] dip '[ _ cuDeviceGet cuda-error ] with-out-parameters ;

: enumerate-cuda-devices ( -- devices )
    #cuda-devices <iota> [ n>cuda-device ] map ;

: with-each-cuda-device ( quot -- )
    [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline

: cuda-device-properties ( n -- properties )
    [ CUdevprop <struct> ] dip
    [ cuDeviceGetProperties cuda-error ] 2keep drop ;

: cuda-devices ( -- assoc )
    enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;

: cuda-device-name ( n -- string )
    [ 256 [ <byte-array> ] keep ] dip
    [ cuDeviceGetName cuda-error ]
    [ 2drop utf8 alien>string ] 3bi ;

: cuda-device-capability ( n -- pair )
    [ { int int } ] dip
    '[ _ cuDeviceComputeCapability cuda-error ] with-out-parameters
    2array ;

: cuda-device-memory ( n -- bytes )
    [ { uint } ] dip
    '[ _ cuDeviceTotalMem cuda-error ] with-out-parameters ;

: cuda-device-attribute ( attribute n -- n )
    [ { int } ] 2dip
    '[ _ _ cuDeviceGetAttribute cuda-error ] with-out-parameters ;

: cuda-device. ( n -- )
    {
        [ "Device: " write number>string print ]
        [ "Name: " write cuda-device-name print ]
        [ "Memory: " write cuda-device-memory number>string print ]
        [
            "Capability: " write
            cuda-device-capability [ number>string ] map " " join print
        ]
        [ "Properties: " write cuda-device-properties . ]
        [
            "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
            CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
            cuda-device-attribute number>string print
        ]
    } cleave ;

: cuda. ( -- )
    init-cuda
    "CUDA Version: " write cuda-version number>string print nl
    #cuda-devices <iota> [ nl ] [ cuda-device. ] interleave ;

: up/i ( x y -- z )<commit_msg>cuda.devices: Utility word to print all cuda devices.<commit_after>! Copyright (C) 2010 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data alien.strings arrays
assocs byte-arrays classes.struct combinators cuda
cuda.contexts cuda.ffi cuda.libraries fry io io.encodings.utf8
kernel locals math math.order math.parser namespaces
prettyprint sequences ;
IN: cuda.devices

: #cuda-devices ( -- n )
    { int } [ cuDeviceGetCount cuda-error ] with-out-parameters ;

: n>cuda-device ( n -- device )
    [ { CUdevice } ] dip '[ _ cuDeviceGet cuda-error ] with-out-parameters ;

: enumerate-cuda-devices ( -- devices )
    #cuda-devices <iota> [ n>cuda-device ] map ;

: with-each-cuda-device ( quot -- )
    [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline

: cuda-device-properties ( n -- properties )
    [ CUdevprop <struct> ] dip
    [ cuDeviceGetProperties cuda-error ] 2keep drop ;

: cuda-devices ( -- assoc )
    enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;

: cuda-device-name ( n -- string )
    [ 256 [ <byte-array> ] keep ] dip
    [ cuDeviceGetName cuda-error ]
    [ 2drop utf8 alien>string ] 3bi ;

: cuda-device-capability ( n -- pair )
    [ { int int } ] dip
    '[ _ cuDeviceComputeCapability cuda-error ] with-out-parameters
    2array ;

: cuda-device-memory ( n -- bytes )
    [ { uint } ] dip
    '[ _ cuDeviceTotalMem cuda-error ] with-out-parameters ;

: cuda-device-attribute ( attribute n -- n )
    [ { int } ] 2dip
    '[ _ _ cuDeviceGetAttribute cuda-error ] with-out-parameters ;

: cuda-device. ( n -- )
    {
        [ "Device: " write number>string print ]
        [ "Name: " write cuda-device-name print ]
        [ "Memory: " write cuda-device-memory number>string print ]
        [
            "Capability: " write
            cuda-device-capability [ number>string ] map " " join print
        ]
        [ "Properties: " write cuda-device-properties . ]
        [
            "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
            CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
            cuda-device-attribute number>string print
        ]
    } cleave ;

: cuda-devices. ( -- )
    init-cuda
    enumerate-cuda-devices [ cuda-device. ] each ;

: cuda. ( -- )
    init-cuda
    "CUDA Version: " write cuda-version number>string print nl
    #cuda-devices <iota> [ nl ] [ cuda-device. ] interleave ;

: up/i ( x y -- z )
{'fraction_non_alphanumeric': 0.12770426381012392, 'fraction_numerical': 0.00630119722747322, 'mean_word_length': 2.4963289280469896, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '501794', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1587, 'n_words': 356}