ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
extraUtilCube.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25#include "misc/vec/vec.h"
26#include "misc/vec/vecHsh.h"
27
29
33
34static inline void Abc_StatePush( Vec_Int_t * vData, char * pState, int k ) { int i; for ( i = 0; i < 6; i++ ) Vec_IntWriteEntry(vData, 6*k+i, ((int*)pState)[i]); }
35static inline void Abc_StatePerm( char * pState, char * pPerm, char * pRes ) { int i; for ( i = 0; i < 24; i++ ) pRes[i] = pState[(int)pPerm[i]]; }
36static inline void Abc_StatePrint( char * pState ) { int i; for ( i = 0; i < 24; i++ ) printf(" %2d", pState[i]); printf( "\n" ); }
37
41
54{
55 int pXYZ[3][9][2] = {
56 { {3, 5}, {3,17}, {3,15}, {1, 6}, {1,16}, {1,14}, {2, 4}, {2,18}, {2,13} },
57 { {2,14}, {2,24}, {2,12}, {3,13}, {3,23}, {3,10}, {1,15}, {1,22}, {1,11} },
58 { {1,10}, {1, 7}, {1, 4}, {3,12}, {3, 9}, {3, 6}, {2,11}, {2, 8}, {2, 5} } };
59
60 Vec_Int_t * vData = Vec_IntStart( 6 * (1 << 22) ); // 96 MB
61 Hsh_IntMan_t * vHash = Hsh_IntManStart( vData, 6, 1 << 22 ); // 32 MB
62 int i, k, v, RetValue, Beg, End, Counter = 0;
63 char pStart[24], pFirst[9][24];
64 abctime clk = Abc_Clock();
65 printf( "Enumerating states of 2x2x2 cube.\n" );
66 // init state
67 for ( v = 0; v < 24; v++ )
68 pStart[v] = v;
69 Abc_StatePush( vData, pStart, Counter );
70 RetValue = Hsh_IntManAdd( vHash, Counter );
71 assert( RetValue == Counter );
72 Counter++;
73 // first nine states
74 for ( i = 0; i < 3; i++ )
75 {
76 memcpy( pFirst[i], pStart, 24 );
77 for ( v = 0; v < 9; v++ )
78 ABC_SWAP( char, pFirst[i][pXYZ[i][v][0]-1], pFirst[i][pXYZ[i][v][1]-1] );
79 Abc_StatePush( vData, pFirst[i], Counter );
80 RetValue = Hsh_IntManAdd( vHash, Counter );
81 assert( RetValue == Counter );
82 Counter++;
83 //Abc_StatePrint( pFirst[i] );
84
85 memcpy( pFirst[3+i], pFirst[i], 24 );
86 for ( v = 0; v < 9; v++ )
87 ABC_SWAP( char, pFirst[3+i][pXYZ[i][v][0]-1], pFirst[3+i][pXYZ[i][v][1]-1] );
88 Abc_StatePush( vData, pFirst[3+i], Counter );
89 RetValue = Hsh_IntManAdd( vHash, Counter );
90 assert( RetValue == Counter );
91 Counter++;
92 //Abc_StatePrint( pFirst[3+i] );
93
94 memcpy( pFirst[6+i], pFirst[3+i], 24 );
95 for ( v = 0; v < 9; v++ )
96 ABC_SWAP( char, pFirst[6+i][pXYZ[i][v][0]-1], pFirst[6+i][pXYZ[i][v][1]-1] );
97 Abc_StatePush( vData, pFirst[6+i], Counter );
98 RetValue = Hsh_IntManAdd( vHash, Counter );
99 assert( RetValue == Counter );
100 Counter++;
101 //Abc_StatePrint( pFirst[6+i] );
102 }
103 printf( "Iter %2d -> %8d ", 0, 1 );
104 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
105 printf( "Iter %2d -> %8d ", 1, Counter );
106 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
107 Beg = 1; End = 10;
108 for ( i = 2; i <= 100; i++ )
109 {
110 if ( Beg == End )
111 break;
112 for ( k = Beg; k < End; k++ )
113 for ( v = 0; v < 9; v++ )
114 {
115 Abc_StatePerm( (char *)Vec_IntEntryP(vData, 6*k), pFirst[v], (char *)Vec_IntEntryP(vData, 6*Counter) );
116 RetValue = Hsh_IntManAdd( vHash, Counter );
117 if ( RetValue == Counter )
118 Counter++;
119 if ( Counter == (1<<22) )
120 {
121 printf( "Did not converge. " );
122 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
123 return;
124 }
125 }
126 Beg = End; End = Counter;
127 printf( "Iter %2d -> %8d ", i, Counter );
128 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
129 }
130 Hsh_IntManStop( vHash );
131 Vec_IntFree( vData );
132}
133
134/*
135Enumerating states of 2x2x2 cube.
136Iter 0 -> 1 Time = 0.00 sec
137Iter 1 -> 10 Time = 0.00 sec
138Iter 2 -> 64 Time = 0.00 sec
139Iter 3 -> 385 Time = 0.00 sec
140Iter 4 -> 2232 Time = 0.00 sec
141Iter 5 -> 12224 Time = 0.00 sec
142Iter 6 -> 62360 Time = 0.02 sec
143Iter 7 -> 289896 Time = 0.09 sec
144Iter 8 -> 1159968 Time = 0.90 sec
145Iter 9 -> 3047716 Time = 11.62 sec
146Iter 10 -> 3671516 Time = 52.00 sec
147Iter 11 -> 3674160 Time = 70.38 sec
148Iter 12 -> 3674160 Time = 70.48 sec
149*/
150
162static inline int Abc_DataHasBit( word * p, word i ) { return (p[(i)>>6] & (((word)1)<<((i) & 63))) > 0; }
163static inline void Abc_DataXorBit( word * p, word i ) { p[(i)>>6] ^= (((word)1)<<((i) & 63)); }
164static inline int Abc_DataGetCube( word w, int i ) { return (w >> (5*i)) & 31; }
165static inline word Abc_DataXorCube( word w, int i, int c ) { return w ^ (((word)c) << (5*i)); }
166static inline word Abc_CubeGenerateSign( char * pState )
167{
168 static int Var2Cor[24] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, -1, -1, -1, 6, 6, 6 };
169 static int Var2Per[24] = { 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, -1, -1, -1, 0, 1, 2 };
170 word Res = 0;
171 Res = Abc_DataXorCube( Res, 0, (Var2Cor[(int)pState[ 0]] << 2) | Var2Per[(int)pState[ 0]] );
172 Res = Abc_DataXorCube( Res, 1, (Var2Cor[(int)pState[ 3]] << 2) | Var2Per[(int)pState[ 3]] );
173 Res = Abc_DataXorCube( Res, 2, (Var2Cor[(int)pState[ 6]] << 2) | Var2Per[(int)pState[ 6]] );
174 Res = Abc_DataXorCube( Res, 3, (Var2Cor[(int)pState[ 9]] << 2) | Var2Per[(int)pState[ 9]] );
175 Res = Abc_DataXorCube( Res, 4, (Var2Cor[(int)pState[12]] << 2) | Var2Per[(int)pState[12]] );
176 Res = Abc_DataXorCube( Res, 5, (Var2Cor[(int)pState[15]] << 2) | Var2Per[(int)pState[15]] );
177 Res = Abc_DataXorCube( Res, 6, (Var2Cor[(int)pState[21]] << 2) | Var2Per[(int)pState[21]] );
178 return Res;
179}
180
193{
194 int pXYZ[3][9][2] = {
195 { {3, 5}, {3,17}, {3,15}, {1, 6}, {1,16}, {1,14}, {2, 4}, {2,18}, {2,13} },
196 { {2,14}, {2,24}, {2,12}, {3,13}, {3,23}, {3,10}, {1,15}, {1,22}, {1,11} },
197 { {1,10}, {1, 7}, {1, 4}, {3,12}, {3, 9}, {3, 6}, {2,11}, {2, 8}, {2, 5} } };
198
199 Vec_Int_t * vData = Vec_IntStart( 6 * (1 << 22) ); // 96 MB
200 word State, * pHash = ABC_CALLOC( word, 1 << 29 ); // 4 GB
201 int i, k, v, Beg, End, Counter = 0;
202 char * pPrev, * pState, pStart[24], pFirst[9][24];
203 abctime clk = Abc_Clock();
204 printf( "Enumerating states of 2x2x2 cube.\n" );
205 // init state
206 for ( v = 0; v < 24; v++ )
207 pStart[v] = v;
208 Abc_StatePush( vData, pStart, Counter );
209 Abc_DataXorBit( pHash, Abc_CubeGenerateSign(pStart) );
210 Counter++;
211 // first nine states
212 for ( i = 0; i < 3; i++ )
213 {
214 memcpy( pFirst[i], pStart, 24 );
215 for ( v = 0; v < 9; v++ )
216 ABC_SWAP( char, pFirst[i][pXYZ[i][v][0]-1], pFirst[i][pXYZ[i][v][1]-1] );
217 Abc_StatePush( vData, pFirst[i], Counter );
218 Abc_DataXorBit( pHash, Abc_CubeGenerateSign(pFirst[i]) );
219 Counter++;
220 //Abc_StatePrint( pFirst[i] );
221
222 memcpy( pFirst[3+i], pFirst[i], 24 );
223 for ( v = 0; v < 9; v++ )
224 ABC_SWAP( char, pFirst[3+i][pXYZ[i][v][0]-1], pFirst[3+i][pXYZ[i][v][1]-1] );
225 Abc_StatePush( vData, pFirst[3+i], Counter );
226 Abc_DataXorBit( pHash, Abc_CubeGenerateSign(pFirst[3+i]) );
227 Counter++;
228 //Abc_StatePrint( pFirst[3+i] );
229
230 memcpy( pFirst[6+i], pFirst[3+i], 24 );
231 for ( v = 0; v < 9; v++ )
232 ABC_SWAP( char, pFirst[6+i][pXYZ[i][v][0]-1], pFirst[6+i][pXYZ[i][v][1]-1] );
233 Abc_StatePush( vData, pFirst[6+i], Counter );
234 Abc_DataXorBit( pHash, Abc_CubeGenerateSign(pFirst[6+i]) );
235 Counter++;
236 //Abc_StatePrint( pFirst[6+i] );
237 }
238 printf( "Iter %2d -> %8d ", 0, 1 );
239 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
240 printf( "Iter %2d -> %8d ", 1, Counter );
241 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
242 Beg = 1; End = 10;
243 for ( i = 2; i <= 100; i++ )
244 {
245 if ( Beg == End )
246 break;
247 for ( k = Beg; k < End; k++ )
248 {
249 pPrev = (char *)Vec_IntEntryP(vData, 6*k);
250 for ( v = 0; v < 9; v++ )
251 {
252 pState = (char *)Vec_IntEntryP(vData, 6*Counter);
253 Abc_StatePerm( pPrev, pFirst[v], pState );
254 State = Abc_CubeGenerateSign( pState );
255 if ( !Abc_DataHasBit(pHash, State) )
256 Abc_DataXorBit(pHash, State), Counter++;
257 if ( Counter == (1<<22) )
258 {
259 printf( "Did not converge. " );
260 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
261 return;
262 }
263 }
264 }
265 Beg = End; End = Counter;
266 printf( "Iter %2d -> %8d ", i, Counter );
267 Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
268 }
269 Vec_IntFree( vData );
270 ABC_FREE( pHash );
271}
272
276
277
279
#define ABC_SWAP(Type, a, b)
Definition abc_global.h:253
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
void Abc_EnumerateCubeStates2()
FUNCTION DEFINITIONS ///.
void Abc_EnumerateCubeStates()
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
#define assert(ex)
Definition util_old.h:213
char * memcpy()
struct Hsh_IntMan_t_ Hsh_IntMan_t
Definition vecHsh.h:66