|
| 1 | +using SparseMatrixColorings |
| 2 | +using MatrixMarket |
| 3 | +using SparseArrays |
| 4 | +using CSV |
| 5 | +using DataFrames |
| 6 | + |
| 7 | +function color_mtx_file(filepath::String) |
| 8 | + println("\n==============================") |
| 9 | + println("Loading matrix from: $filepath") |
| 10 | + A = mmread(filepath) |
| 11 | + |
| 12 | + println("Matrix size: ", size(A), ", nnz = ", nnz(A)) |
| 13 | + |
| 14 | + problem = ColoringProblem(; structure = :nonsymmetric, partition = :column) |
| 15 | + algo = GreedyColoringAlgorithm(; decompression = :direct) |
| 16 | + |
| 17 | + # Measure time |
| 18 | + t = @elapsed result = coloring(A, problem, algo) |
| 19 | + |
| 20 | + ncolors = maximum(column_colors(result)) |
| 21 | + |
| 22 | + # Report results |
| 23 | + println("Coloring took $(round(t, digits=4)) seconds") |
| 24 | + println("Number of colors used: $ncolors") |
| 25 | + |
| 26 | + return (rows=size(A,1), cols=size(A,2), nnz=nnz(A), time=t, ncolors=ncolors) |
| 27 | +end |
| 28 | + |
| 29 | +# Run from CSV and save results |
| 30 | +function run_from_csv(input_csv::String, output_csv::String) |
| 31 | + df = CSV.read(input_csv, DataFrame) |
| 32 | + |
| 33 | + results = DataFrame( |
| 34 | + Matrix=String[], |
| 35 | + Rows=Int[], |
| 36 | + Cols=Int[], |
| 37 | + NNZ=Int[], |
| 38 | + TimeJulia=Float64[], |
| 39 | + NumberOfColorsJulia=Int[] |
| 40 | + ) |
| 41 | + |
| 42 | + for row in eachrow(df) |
| 43 | + filepath = row.Matrix |
| 44 | + try |
| 45 | + stats = color_mtx_file(filepath) |
| 46 | + push!(results, ( |
| 47 | + filepath, |
| 48 | + stats.rows, |
| 49 | + stats.cols, |
| 50 | + stats.nnz, |
| 51 | + stats.time, |
| 52 | + stats.ncolors |
| 53 | + )) |
| 54 | + catch e |
| 55 | + @warn "Failed to process $filepath" exception=(e, catch_backtrace()) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + CSV.write(output_csv, results) |
| 60 | + println("\n✅ Results written to $output_csv") |
| 61 | +end |
| 62 | + |
| 63 | +# Example usage |
| 64 | +run_from_csv("input.csv", "Output/output_julia.csv") |
0 commit comments