// use std::io::{self, BufRead}; // use rayon::prelude::*; use sqlx::migrate::MigrateDatabase; // use std::sync::{Arc, Mutex}; // use std::time::Instant; use tokio; mod graph; mod geng; use crate::geng::GengIterator; mod compute; #[tokio::main] async fn main() -> Result<(), sqlx::Error> { dotenv::dotenv().ok(); let database_url = std::env::var("DATABASE_URL").expect("Expected DATABASE_URL in env"); if !sqlx::Sqlite::database_exists(&database_url).await? { sqlx::Sqlite::create_database(&database_url).await?; } let db = sqlx::SqlitePool::connect(&database_url).await?; let _ = sqlx::query!( "CREATE TABLE IF NOT EXISTS graphs (g6 VARCHAR NOT NULL, ind_dom INT NOT NULL, forced_geod INT NOT NULL);" ) .execute(&db) .await; let gi = GengIterator::new(6); let mut res = Vec::new(); const BATCH_SIZE: usize = 10000; loop { let graphs = gi.take(BATCH_SIZE); let tasks: Vec<_> = graphs .map(|g| { let db = db.clone(); tokio::spawn(async move { let (g6, ind_dom, fg) = compute::dominating_numbers(g).await; let ind_dom = ind_dom.unwrap_or(0); let fg = fg.unwrap_or(0); let _ = sqlx::query!( "INSERT INTO graphs (g6, ind_dom, forced_geod) VALUES (?, ?, ?);", g6, ind_dom, fg ).execute(&db).await; }) }) .collect(); let mut part = futures::future::join_all(tasks).await; if part.len() < BATCH_SIZE { res.append(&mut part); break; } res.append(&mut part); } // let elapsed = start.elapsed(); // let time = elapsed.as_nanos(); // let res = res.iter().map(|e| e.as_ref().unwrap()).collect::>(); println!("len = {}", res.len()); // println!("Time elapsed: {}s", time as f64 / 1e9); // let res: Vec<_> = // gi.par_bridge().map(compute::dominating_numbers).collect(); // let res: Vec<_> = gi.map(compute::dominating_numbers).collect(); // for pair in &res { // if let Some(cardinality) = pair.1 { // println!("{} {:?} {:?}", pair.0, cardinality, pair.2); // } // } // let start = Instant::now(); // let res = gi // .par_bridge() // .map(compute::apply_theorems) // .collect::>(); // // let elapsed = start.elapsed(); // let time = elapsed.as_nanos(); // println!("len = {}", res.len()); // println!("Time elapsed: {}s", time as f64 / 1e9); Ok(()) }