Allows setting of cores to use (sorta fixes #8)

This commit is contained in:
Sacha Ligthert 2025-01-25 19:45:31 +01:00
parent 6d6db0ed28
commit 28ecc74d7a
3 changed files with 23 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"runtime"
)
func (solver *Solver) parse_flags() {
@ -30,10 +31,21 @@ func (solver *Solver) parse_flags() {
flag.StringVar(&row7, "row7", "000000000", "7th row of the sudoku puzzle.")
flag.StringVar(&row8, "row8", "000000000", "8th row of the sudoku puzzle.")
flag.StringVar(&row9, "row9", "000000000", "9th row of the sudoku puzzle.")
flag.IntVar(&solver.numcpus, "numcpu", runtime.NumCPU(), "Number of CPU cores to assign to this task.")
// Parse the flags
flag.Parse()
if solver.numcpus <= 0 {
log.Printf("ERROR: Number of CPU cores must be 1 or higher.\n\n")
solver.print_Usage()
os.Exit(1)
}
if solver.numcpus != runtime.NumCPU() {
runtime.GOMAXPROCS(solver.numcpus)
}
if row1 == "000000000" || row2 == "000000000" || row3 == "000000000" || row4 == "000000000" || row5 == "000000000" || row6 == "000000000" || row7 == "000000000" || row8 == "000000000" || row9 == "000000000" {
log.Printf("ERROR: All parameters must be entered.\n\n")
solver.print_Usage()

View File

@ -2,6 +2,8 @@ package solver
import (
"log"
"runtime"
"strconv"
)
func Run() {
@ -11,6 +13,11 @@ func Run() {
// Parse and handle flags
solver.parse_flags()
// Report number of CPUs being used, if set.
if runtime.NumCPU() != solver.numcpus {
log.Println("Using " + strconv.Itoa(solver.numcpus) + " CPUs, (was " + strconv.Itoa(runtime.NumCPU()) + ")")
}
// Load blocks from CSV file
solver.load_blocks()

View File

@ -1,6 +1,8 @@
package solver
import "sync/atomic"
import (
"sync/atomic"
)
type Solver struct {
blocks []int
@ -26,4 +28,5 @@ type Solver struct {
counter atomic.Int64
solutions []string
rates []int64
numcpus int
}