text
stringlengths
0
601k
type usymbol_provenance = { original_idents : Ident . t list ; module_path : Path . t ; }
type uconstant_block_field = | Uconst_field_ref of string | Uconst_field_int of int
type preallocated_block = { symbol : string ; exported : bool ; tag : int ; fields : uconstant_block_field option list ; provenance : usymbol_provenance option ; }
type preallocated_constant = { symbol : string ; exported : bool ; definition : ustructured_constant ; provenance : usymbol_provenance option ; }
type with_constants = ulambda * preallocated_block list * preallocated_constant list
let compare_floats x1 x2 = Int64 . compare ( Int64 . bits_of_float x1 ) ( Int64 . bits_of_float x2 )
let rec compare_float_lists l1 l2 = match l1 , l2 with | [ ] , [ ] -> 0 | [ ] , _ :: _ -> - 1 | _ :: _ , [ ] -> 1 | h1 :: t1 , h2 :: t2 -> let c = compare_floats h1 h2 in if c <> 0 then c else compare_float_lists t1 t2
let compare_constants c1 c2 = match c1 , c2 with | Uconst_ref ( lbl1 , _c1 ) , Uconst_ref ( lbl2 , _c2 ) -> String . compare lbl1 lbl2 | Uconst_int n1 , Uconst_int n2 -> Stdlib . compare n1 n2 | Uconst_ref _ , _ -> - 1 | Uconst_int _ , Uconst_ref _ -> 1
let rec compare_constant_lists l1 l2 = match l1 , l2 with | [ ] , [ ] -> 0 | [ ] , _ :: _ -> - 1 | _ :: _ , [ ] -> 1 | h1 :: t1 , h2 :: t2 -> let c = compare_constants h1 h2 in if c <> 0 then c else compare_constant_lists t1 t2
let rank_structured_constant = function | Uconst_float _ -> 0 | Uconst_int32 _ -> 1 | Uconst_int64 _ -> 2 | Uconst_nativeint _ -> 3 | Uconst_block _ -> 4 | Uconst_float_array _ -> 5 | Uconst_string _ -> 6 | Uconst_closure _ -> 7
let compare_structured_constants c1 c2 = match c1 , c2 with | Uconst_float x1 , Uconst_float x2 -> compare_floats x1 x2 | Uconst_int32 x1 , Uconst_int32 x2 -> Int32 . compare x1 x2 | Uconst_int64 x1 , Uconst_int64 x2 -> Int64 . compare x1 x2 | Uconst_nativeint x1 , Uconst_nativeint x2 -> Nativeint . compare x1 x2 | Uco...
type boxed = | Boxed | Unboxed
type memory_access_size = | Sixteen | Thirty_two | Sixty_four
type primitive = | Pread_symbol of string | Pmakeblock of int * mutable_flag * block_shape * alloc_mode | Pfield of int | Pfield_computed | Psetfield of int * immediate_or_pointer * initialization_or_assignment | Psetfield_computed of immediate_or_pointer * initialization_or_assignment | Pfloatfield of int * alloc_mode...
let equal ( x : primitive ) ( y : primitive ) = x = y
let comp_ml = kern trainingSet data res setSize dataSize -> let open Std in let tid = thread_idx_x + block_dim_x * block_idx_x in if tid < setSize then ( let mutable diff = 0 in let mutable toAdd = 0 in let mutable i = 0 in while ( i < dataSize ) do toAdd := data . [ < i ] > - trainingSet . [ < tid * dataSize + i ] ; >...
type labelPixels = { label : int ; pixels : int array }
let devices = Spoc . Devices . init ( )
let dev = ref devices . ( try int_of_string Sys . argv . ( 1 ) with | _ -> 0 )
let auto_transfers = ref true
let compute_gpu = ref true
let trainSet_size = ref 5000
let data_size = ref 784
let trainSet = Spoc . Vector . create Spoc . Vector . int32 ( ! trainSet_size * ! data_size )
let results = Spoc . Vector . create Spoc . Vector . int32 ! trainSet_size
let vect_validation = Spoc . Vector . create Spoc . Vector . int32 ! data_size
let block = { Spoc . Kernel . blockX = 1024 ; Spoc . Kernel . blockY = 1 ; Spoc . Kernel . blockZ = 1 ; }
let grid = { Spoc . Kernel . gridX = 5 ; Spoc . Kernel . gridY = 1 ; Spoc . Kernel . gridZ = 1 ; }
let parse_args ( ) = let arg1 = ( " - device " , Arg . Int ( fun i -> dev := devices . ( i ) ) , " number of the device [ 0 ] " ) and arg2 = ( " - auto " , Arg . Bool ( fun b -> auto_transfers := b ) , " let Spoc handles transfers automatically [ false ] " ) and arg3 = ( " - compute " , Arg . Bool ( fun b -> compute_gp...
let load_data_to_GPU data = Printf . printf " Will use device : % s \ n " ( ! dev ) . Spoc . Devices . general_info . Spoc . Devices . name ; Printf . printf " Loading a vector with the training data \ n " ; %! let f = ( fun index elem -> let pix_list = elem . pixels in for i = 0 to ! data_size - 1 do Spoc . Mem . set ...
let read_lines name : string list = Printf . printf " Reading data from file : % s \ n " %! name ; let ic = open_in name in let try_read ( ) = try Some ( input_line ic ) with End_of_file -> None in let rec loop acc = match try_read ( ) with | Some s -> loop ( s :: acc ) | None -> close_in ic ; List . rev acc in loop [ ...
let slurp_file file = List . tl ( read_lines file ) |> List . map ( fun line -> Str . split ( Str . regexp " , " ) line ) |> List . map ( fun numline -> List . map ( fun ( x : string ) -> int_of_string x ) numline ) |> List . map ( fun line -> { label = List . hd line ; pixels = Array . of_list @@ List . tl line } ) |>...
let array_fold_left2 f acc a1 a2 = let open Array in let len = length a1 in let rec iter acc i = if i = len then acc else let v1 = unsafe_get a1 i in let v2 = unsafe_get a2 i in iter ( f acc v1 v2 ) ( i + 1 ) in iter acc 0
let distance p1 p2 = sqrt @@ float_of_int @@ array_fold_left2 ( fun acc a b -> let d = a - b in acc + d * d ) 0 p1 p2
let classify ( pixels : int array ) trainingset = fst ( Array . fold_left ( fun ( ( min_label , min_dist ) as min ) ( x : labelPixels ) -> let dist = distance pixels x . pixels in if dist < min_dist then ( x . label , dist ) else min ) ( max_int , max_float ) trainingset )
let r = ref true
let classify_gpu ( pixels : int array ) trainingset = Array . iteri ( fun i x -> Spoc . Mem . set vect_validation i ( Int32 . of_int x ) ) pixels ; if ( not ! auto_transfers ) then Spoc . Mem . to_device vect_validation ! dev ; if ! r then ( r := not ! r ; Kirc . profile_run comp_ml ( trainSet , vect_validation , resul...
let num_correct trainingset validationsample = load_data_to_GPU trainingset ; if ! compute_gpu then Array . fold_left ( fun sum p -> sum + if classify_gpu p . pixels trainingset = p . label then 1 else 0 ) 0 validationsample else Array . fold_left ( fun sum p -> sum + if classify p . pixels trainingset = p . label then...
let _ = parse_args ( ) ; let validationsample = slurp_file " . / validationsample . csv " in let trainingset = slurp_file " . / trainingsample . csv " in Printf . printf " \ nResults with training set of % i vectors of length % i \ n " %! ! trainSet_size ! data_size ; Printf . printf " Percentage correct :% f \ n " ( (...
module Make ( Ast : Sig . Camlp4Ast ) = struct class clean_ast = object inherit Ast . map as super ; method with_constr wc = match super # with_constr wc with [ <: with_constr < $ <: with_constr <>> $ and $ wc $ >> | <: with_constr < $ wc $ and $ <: with_constr <>> $ >> -> wc | wc -> wc ] ; method expr e = match super ...
let objfiles = ref ( [ ] : string list )
let compile_only = ref false
let annotations = ref false
let dump_parsetree = ref false
let keep_asm_file = ref false
let optimize_for_speed = ref true
let register_allocator = ref Config . default_register_allocator
let keep_startup_file = ref false
let native_code = ref false
let inline_threshold = ref 10
let dont_write_files = ref false
let std_include_flag prefix = if ! no_std_include then " " else ( prefix ^ ( Filename . quote Config . standard_library ) ) ; ;
let std_include_dir ( ) = if ! no_std_include then [ ] else [ Config . standard_library ] ; ;
let shared = ref false
let dlcode = ref true
module Constants = struct let runtime_api_version = " 2018 - 06 - 01 " let api_content_type = " application / json " let api_error_content_type = " application / vnd . aws . lambda . error + json " let runtime_error_header = " Lambda - Runtime - Function - Error - Type " module RequestHeaders = struct let request_id = ...
type client_application = { installation_id : string ; app_title : string option [ @ default None ] ; app_version_name : string ; app_version_code : string ; app_package_name : string }
type client_context = { client : client_application ; custom : Yojson . Safe . t ; env : Yojson . Safe . t }
type cognito_identity = { identity_id : string ; identity_pool_id : string }
type event_context = { invoked_function_arn : string ; aws_request_id : string ; xray_trace_id : string option [ @ default None ] ; deadline : int64 ; client_context : client_context option ; identity : cognito_identity option }
let make endpoint = let uri = Uri . of_string ( Format . asprintf " http ://% s " endpoint ) in Piaf . Client . create uri >|= function | Ok connection -> Ok connection | Error msg -> Error msg
let make_runtime_post_request client path output = let body = Yojson . Safe . to_string output in Piaf . Client . post client ~ headers [ : " Content - Type " , Constants . api_content_type ] ~ body ( : Piaf . Body . of_string body ) path
let event_response client request_id output = let open Piaf in let path = Format . asprintf " /% s / runtime / invocation /% s / response " Constants . runtime_api_version request_id in make_runtime_post_request client path output >>= function | Ok { Response . status ; _ } -> if not ( Status . is_successful status ) t...
let make_runtime_error_request connection path error = let open Piaf in let body = Errors . to_lambda_error error |> Yojson . Safe . to_string in Client . post connection ~ headers : [ " Content - Type " , Constants . api_error_content_type ; Constants . runtime_error_header , " RuntimeError " ] ~ body ( : Body . of_st...
let event_error client request_id err = let open Piaf in let path = Format . asprintf " /% s / runtime / invocation /% s / error " Constants . runtime_api_version request_id in make_runtime_error_request client path err >>= function | Ok { Response . status ; _ } -> if not ( Status . is_successful status ) then let err...
let fail_init client err = let path = Format . asprintf " /% s / runtime / init / error " Constants . runtime_api_version in make_runtime_error_request client path err >>= function | Ok _ -> Lwt_result . return ( ) | Error _ -> failwith " Error while sending init failed message "
let get_event_context headers = let open Piaf in let report_error header = let err = Errors . make_api_error ~ recoverable : true ( Printf . sprintf " Missing % s header " header ) in Error err in let open Constants in match Headers . get headers RequestHeaders . request_id with | None -> report_error RequestHeaders . ...
let next_event client = let open Piaf in let path = Format . asprintf " /% s / runtime / invocation / next " Constants . runtime_api_version in Logs_lwt . info ( fun m -> m " Polling for next event . Path : % s \ n " path ) >>= fun ( ) -> Client . get client path >>= function | Ok { Response . status ; headers ; body ;...
let with_channels f = let section = Lwt_log_core . Section . make " with_channels " in let make_channel name ( ic , oc ) = let read c = let len = Cstruct . len c in Lwt_log . debug_f ~ section " % s read : % d " name len >>= fun ( ) -> let b = Bytes . create len in Lwt_io . read_into_exactly ic b 0 len >>= fun ( ) -> C...
let test ~ server ~ client ( ) = Lwt_log . add_rule " " * Lwt_log . Debug ; with_channels ( fun client_channel server_channel -> let test_server = server server_channel in let cancel , _ = Lwt . task ( ) in let test_server = Lwt . catch ( fun ( ) -> Lwt . pick [ test_server ; cancel ] ) ( function Lwt . Canceled -> Lwt...
let check msg = function | Result . Ok a -> Lwt . return a | Result . Error _ -> Lwt . fail_with msg
let test_connect_disconnect _switch = let test_block = Cstruct . of_string " asdf " in test ~ server ( : fun server_channel -> Nbd . Server . connect server_channel ( ) >>= fun ( export_name , svr ) -> Alcotest . ( check string ) " export name received by server " " export1 " export_name ; Nbd . Server . serve svr ~ re...
let test_list_exports _switch = test ~ server ( : fun server_channel -> Lwt . catch ( fun ( ) -> Nbd . Server . connect ~ offer [ " : export1 " ; " export2 " ] server_channel ( ) >>= fun _ -> Alcotest . fail " Server should not enter transmission mode " ) ( function | Nbd . Server . Client_requested_abort -> Lwt . retu...
let test_read_write _switch = let test_block = Cstruct . of_string " asdf " in test ~ server ( : fun server_channel -> Nbd . Server . connect server_channel ( ) >>= fun ( _export_name , svr ) -> Nbd . Server . serve svr ~ read_only : false ( module Cstruct_block . Block ) test_block ) ~ client ( : fun client_channel ->...
let tests = let t = Alcotest_lwt . test_case in ( " Nbd client - server connection tests " , [ t " test_connect_disconnect " ` Quick test_connect_disconnect ; t " test_list_exports " ` Quick test_list_exports ; t " test_read_write " ` Quick test_read_write ] )
let pi = acos ( - 1 . ) val canvas = Canvas . create ~ width : 100 ~ height : 100 parent val mutable height = 100 val mutable width = 100 val mutable rflag = - 1 method x x0 = truncate ( float width . * ( x0 . + 1 . ) . / 2 . ) method y y0 = truncate ( float height . * ( y0 . + 1 . ) . / 2 . ) initializer Canvas . crea...
let top = openTk ( )
let clock = new clock ~ parent : top
let _ = mainLoop ( )
module Storer = Switch . Store ( struct type t = lambda type key = lambda let make_key = Lambda . make_key let compare_key = Stdlib . compare end )
let no_phantom_lets ( ) = Misc . fatal_error " Closure does not support phantom let generation "
let rec split_list n l = if n <= 0 then ( [ ] , l ) else begin match l with [ ] -> fatal_error " Closure . split_list " | a :: l -> let ( l1 , l2 ) = split_list ( n - 1 ) l in ( a :: l1 , l2 ) end
let rec build_closure_env env_param pos = function [ ] -> V . Map . empty | id :: rem -> V . Map . add id ( Uprim ( P . Pfield pos , [ Uvar env_param ] , Debuginfo . none ) ) ( build_closure_env env_param ( pos + 1 ) rem )
let getglobal dbg id = Uprim ( P . Pread_symbol ( Compilenv . symbol_for_global id ) , [ ] , dbg )
let region ulam = let is_trivial = match ulam with | Uvar _ | Uconst _ -> true | _ -> false in if is_trivial then ulam else Uregion ulam
let tail ulam = let is_trivial = match ulam with | Uvar _ | Uconst _ -> true | _ -> false in if is_trivial then ulam else Utail ulam
let occurs_var var u = let rec occurs = function Uvar v -> v = var | Uconst _ -> false | Udirect_apply ( _lbl , args , _ , _ , _ ) -> List . exists occurs args | Ugeneric_apply ( funct , args , _ , _ ) -> occurs funct || List . exists occurs args | Uclosure ( _fundecls , clos ) -> List . exists occurs clos | Uoffset ( ...
let prim_size prim args = let open Clambda_primitives in match prim with | Pread_symbol _ -> 1 | Pmakeblock _ -> 5 + List . length args | Pfield _ -> 1 | Psetfield ( _f , isptr , init ) -> begin match init with | Root_initialization -> 1 | Assignment _ | Heap_initialization -> match isptr with | Pointer -> 4 | Immediat...
let lambda_smaller lam threshold = let size = ref 0 in let rec lambda_size lam = if ! size > threshold then raise Exit ; match lam with Uvar _ -> ( ) | Uconst _ -> incr size | Udirect_apply ( _ , args , None , _ , _ ) -> size := ! size + 4 ; lambda_list_size args | Udirect_apply _ -> ( ) | Ugeneric_apply ( fn , args , ...
let is_pure_prim p = let open Semantics_of_primitives in match Semantics_of_primitives . for_primitive p with | ( No_effects | Only_generative_effects ) , _ -> true | Arbitrary_effects , _ -> false
let rec is_pure = function Uvar _ -> true | Uconst _ -> true | Uprim ( p , args , _ ) -> is_pure_prim p && List . for_all is_pure args | Uoffset ( arg , _ ) -> is_pure arg | Ulet ( Immutable , _ , _var , def , body ) -> is_pure def && is_pure body | Uregion body -> is_pure body | Utail body -> is_pure body | _ -> false
let make_const c = ( Uconst c , Value_const c )
let make_const_ref c = make_const ( Uconst_ref ( Compilenv . new_structured_constant ~ shared : true c , Some c ) )
let make_const_int n = make_const ( Uconst_int n )
let make_const_bool b = make_const_int ( if b then 1 else 0 )
let make_integer_comparison cmp x y = let open Clambda_primitives in make_const_bool ( match cmp with Ceq -> x = y | Cne -> x <> y | Clt -> x < y | Cgt -> x > y | Cle -> x <= y | Cge -> x >= y )
let make_float_comparison cmp x y = make_const_bool ( match cmp with | CFeq -> x = y | CFneq -> not ( x = y ) | CFlt -> x < y | CFnlt -> not ( x < y ) | CFgt -> x > y | CFngt -> not ( x > y ) | CFle -> x <= y | CFnle -> not ( x <= y ) | CFge -> x >= y | CFnge -> not ( x >= y ) )
let make_const_float n = make_const_ref ( Uconst_float n )
let make_const_natint n = make_const_ref ( Uconst_nativeint n )
let make_const_int32 n = make_const_ref ( Uconst_int32 n )