context.rb 2.74 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1
module CCS
2
3
4
5
6
7
  attach_function :ccs_context_get_name, [:ccs_context_t, :pointer], :ccs_result_t
  attach_function :ccs_context_get_user_data, [:ccs_context_t, :pointer], :ccs_result_t
  attach_function :ccs_context_get_num_hyperparameters, [:ccs_context_t, :pointer], :ccs_result_t
  attach_function :ccs_context_get_hyperparameter, [:ccs_context_t, :size_t, :pointer], :ccs_result_t
  attach_function :ccs_context_get_hyperparameter_by_name, [:ccs_context_t, :string, :pointer], :ccs_result_t
  attach_function :ccs_context_get_hyperparameter_index_by_name, [:ccs_context_t, :string, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
8
  attach_function :ccs_context_get_hyperparameter_index, [:ccs_context_t, :ccs_hyperparameter_t, :pointer], :ccs_result_t
9
10
  attach_function :ccs_context_get_hyperparameter_indexes, [:ccs_context_t, :size_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_context_get_hyperparameters, [:ccs_context_t, :size_t, :pointer, :pointer], :ccs_result_t
Brice Videau's avatar
Brice Videau committed
11
  class Context < Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    add_property :user_data, :pointer, :ccs_context_get_user_data, memoize: true
    add_property :num_hyperparameters, :size_t, :ccs_context_get_num_hyperparameters, memoize: false

    def name
      @name ||= begin
        ptr = MemoryPointer::new(:pointer)
        res = CCS.ccs_context_get_name(@handle, ptr)
        CCS.error_check(res)
        ptr.read_pointer.read_string
      end
    end

    def hyperparameter(index)
      ptr = MemoryPointer::new(:ccs_hyperparameter_t)
      res = CCS.ccs_context_get_hyperparameter(@handle, index, ptr)
      CCS.error_check(res)
      Hyperparameter.from_handle(ptr.read_ccs_hyperparameter_t)
    end

    def hyperparameter_by_name(name)
32
      name = name.inspect if name.kind_of?(Symbol)
33
34
35
36
37
38
39
      ptr = MemoryPointer::new(:ccs_hyperparameter_t)
      res = CCS.ccs_context_get_hyperparameter_by_name(@handle, name, ptr)
      CCS.error_check(res)
      Hyperparameter.from_handle(ptr.read_ccs_hyperparameter_t)
    end

    def hyperparameter_index_by_name(name)
40
      name = name.inspect if name.kind_of?(Symbol)
41
42
43
44
45
46
      ptr = MemoryPointer::new(:size_t)
      res = CCS.ccs_context_get_hyperparameter_index_by_name(@handle, name, ptr)
      CCS.error_check(res)
      ptr.read_size_t
    end

Brice Videau's avatar
Brice Videau committed
47
48
49
50
51
52
    def hyperparameter_index(hyperparameter)
      ptr = MemoryPointer::new(:size_t)
      res = CCS.ccs_context_get_hyperparameter_index(@handle, hyperparameter, ptr)
      CCS.error_check(res)
      ptr.read_size_t
    end
53
54
55
56
57
58
59
60
61
62

    def hyperparameters
      count = num_hyperparameters
      return [] if count == 0
      ptr = MemoryPointer::new(:ccs_hyperparameter_t, count)
      res = CCS.ccs_context_get_hyperparameters(@handle, count, ptr, nil)
      CCS.error_check(res)
      count.times.collect { |i| Hyperparameter.from_handle(ptr[i].read_pointer) }
    end

Brice Videau's avatar
Brice Videau committed
63
64
  end
end