/* N-QUEENS problem, Fork95 (V1.7) implementation 9710 C. Kessler */ #include #include #include #include sh int N; /* the number of queens */ #define PRINT_SOLUTIONS 0 sh simple_lock screen = 0; /*lock for screen output*/ sh int solutions = 0; /*counter for number of solutions*/ #if PRINT_SOLUTIONS /* output an array q of n column indices of the queens ordered by row; * with an extra column index k for row n, and count the solutions: */ async void print_queens( pr int *q, pr int n, pr int k ) { pr int j; simple_lockup( &screen ); mpadd( &solutions, 1 ); printf("Solution %d: ", solutions); for (j=0; j=4 || (waittime++ > maxtime)); /* start-condition */ __RANK__ < 8 ) /* stayinside-cond */ print_p_solutions(q,n,k); /*body = bus tour*/ else continue; } #endif /** q is allocated with N entries, n fail if (q[i]-j == n-i) feasible = 0; // -> fail } if (!feasible) return; if (n __STARTED_PROCS__) { printf("You should run at least %d processors\n", N); exit(0); } printf("\nComputing solutions to the %d-Queens problem...\n\n", N); } start { pprintf("$$=%d\n", $$); /*for the first queen all column positions in row 0 are feasible: */ initTracing( 100000 ); startTracing(); starttime = getct(); asm("stg fpp,%Fpp"); pprintf("$$=%d fpp=%p gps=%p\n", $$,Fpp,Gps); fork( N; @=$%N; ) { asm("stg fpp,%Fpp"); asm("stg gps,%Gps"); pprintf("before entry: $$=%d fpp=%p gps=%p\n", $$,Fpp, Gps); test_queen( NULL, 0, @ ); asm("stg fpp,%Fpp"); asm("stg gps,%Gps"); pprintf("after exit: $$=%d, fpp=%p ct=%d\n", $$, Fpp, getct()); } asm("stg fpp,%Fpp"); asm("stg gps,%Gps"); pprintf("after fork: $$=%d, fpp=%p ct=%d\n", $$, Fpp, getct()); stopTracing(); stoptime = getct(); seq { printf("Solutions: %d\nTIME: %d cc\n", solutions, stoptime - starttime ); printf("[Press RETURN to continue]"); scanf("%c", &N ); } writeTraceFile("queens.trv", "synchronous N-Queens implementation"); pprintf("$$=%d\n", $$); } exit(0); }