@@ -51,3 +51,75 @@ function solve(gp::GraphProblem, task; usecuda=false, kwargs...)
5151 error (" unknown task $task ." )
5252 end
5353end
54+
55+ export save_configs, load_configs
56+ using DelimitedFiles
57+ function save_configs (filename, data:: ConfigEnumerator{N,S,C} ; format:: Symbol = :binary ) where {N,S,C}
58+ if format == :binary
59+ write (filename, raw_matrix (data))
60+ elseif format == :text
61+ writedlm (filename, plain_matrix (data))
62+ else
63+ error (" format must be `:binary` or `:text`, got `:$format `" )
64+ end
65+ end
66+ function load_configs (filename; len= nothing , format:: Symbol = :binary , nflavors= 2 )
67+ if format == :binary
68+ len === nothing && error (" you need to specify `len` for reading configurations from binary files." )
69+ S = ceil (Int, log2 (nflavors))
70+ C = _nints (len, S)
71+ return _from_raw_matrix (StaticElementVector{len,S,C}, reshape (reinterpret (UInt64, read (filename)),C,:))
72+ elseif format == :text
73+ return from_plain_matrix (readdlm (filename); nflavors= nflavors)
74+ else
75+ error (" format must be `:binary` or `:text`, got `:$format `" )
76+ end
77+ end
78+
79+ function raw_matrix (x:: ConfigEnumerator{N,S,C} ) where {N,S,C}
80+ m = zeros (UInt64, C, length (x))
81+ @inbounds for i= 1 : length (x), j= 1 : C
82+ m[j,i] = x. data[i]. data[j]
83+ end
84+ return m
85+ end
86+ function plain_matrix (x:: ConfigEnumerator{N,S,C} ) where {N,S,C}
87+ m = zeros (UInt8, N, length (x))
88+ @inbounds for i= 1 : length (x), j= 1 : N
89+ m[j,i] = x. data[i][j]
90+ end
91+ return m
92+ end
93+
94+ function from_raw_matrix (m; len, nflavors= 2 )
95+ S = ceil (Int,log2 (nflavors))
96+ C = size (m, 1 )
97+ T = StaticElementVector{len,S,C}
98+ @assert len* S <= C* 64
99+ _from_raw_matrix (T, m)
100+ end
101+ function _from_raw_matrix (:: Type{StaticElementVector{N,S,C}} , m:: AbstractMatrix ) where {N,S,C}
102+ data = zeros (StaticElementVector{N,S,C}, size (m, 2 ))
103+ @inbounds for i= 1 : size (m, 2 )
104+ data[i] = StaticElementVector {N,S,C} (NTuple {C,UInt64} (view (m,:,i)))
105+ end
106+ return ConfigEnumerator (data)
107+ end
108+ function from_plain_matrix (m:: Matrix ; nflavors= 2 )
109+ S = ceil (Int,log2 (nflavors))
110+ N = size (m, 1 )
111+ C = _nints (N, S)
112+ T = StaticElementVector{N,S,C}
113+ _from_plain_matrix (T, m)
114+ end
115+ function _from_plain_matrix (:: Type{StaticElementVector{N,S,C}} , m:: AbstractMatrix ) where {N,S,C}
116+ data = zeros (StaticElementVector{N,S,C}, size (m, 2 ))
117+ @inbounds for i= 1 : size (m, 2 )
118+ data[i] = convert (StaticElementVector{N,S,C}, view (m, :, i))
119+ end
120+ return ConfigEnumerator (data)
121+ end
122+
123+ # convert to Matrix
124+ Base. Matrix (ce:: ConfigEnumerator ) = plain_matrix (ce)
125+ Base. Vector (ce:: StaticElementVector ) = collect (ce)
0 commit comments