#!/bin/bash
#this runs tvpack followed by vpr
star_var="*"
post_var=".blif"
alpha=0
beta=0.6

echo bench,N,I,alpha,beta,power_clustering,power_vpr,channel_width,energy,critical_path>>simulation_outputs.csv

for bench in ${star_var}${post_var}
do
  bench_name=${bench%.blif}

  for N in 4
  do
  
  	if [ ${N} -eq 1 ]; then
  		I=4
  		ARCH_FILE="archN1.arch"
  	fi
  	if [ ${N} -eq 4 ]; then
  		I=10
  		ARCH_FILE="4x4lut_sanitized.arch"
  	fi
  	if [ ${N} -eq 8 ]; then
  		I=18
  		ARCH_FILE="archN8.arch"
  	fi

		rm ${bench_name}.clust_outp
		#rm -> remove
		# Clustering

		# t-vpack's clustering command	
		#./p-t-vpack ${bench_name}.blif ${bench_name}.net -activity_in ${bench_name}.act -activity_out ${bench_name}.ac2 -function_out ${bench_name}.fun -cluster_size ${N} -inputs_per_cluster ${I} -alpha ${alpha} -beta ${beta} -power_driven off > ${bench_name}.clust_outp

		# p-t-vpack's clustering command	
		./p-t-vpack ${bench_name}.blif ${bench_name}.net -activity_in ${bench_name}.act -activity_out ${bench_name}.ac2 -function_out ${bench_name}.fun -cluster_size ${N} -inputs_per_cluster ${I} -alpha ${alpha} -beta ${beta} > ${bench_name}.clust_outp

		#clustering ${bench_name}.blif ${bench_name}.net -cluster_size ${N} -inputs_per_cluster ${I} > ${bench_name}.clust_outp

        	if [[ -f ${bench_name}.clust_outp && -r ${bench_name}.clust_outp ]]
       		then
		# -f checks if it is a regular file and -r checks if it has read permission
			
			
			clustering_power=$(awk '/Total.Power:/ {print $3}' ./${bench_name}.clust_outp)
			#pin_count=$(gawk '/thresh_length:/ {print $2}' ./${bench_name}.clust_outp)

#i assume that, if it finds the pattern mentioned above "total intercluster nets"(in this case) from the input file benchname.calculated, it assigns the value in the 4th column of that line to the num_nets variable.
			
	 		#START=0
	 		#END=0
	 		#PLACE_TIME=0
			#START=$(date +%s)
			# this gives the start time . the current time			
			#rm ${bench_name}.vpr_outp
			# Placement

			#pvpr command
			#./../pvpr/vpr ${bench_name}.net ./../pvpr/${ARCH_FILE} ${bench_name}.p ${bench_name}.r -activity_file ${bench_name}.ac2 -function_file ${bench_name}.fun -nodisp > ${bench_name}.vpr_outp

			#vpr command
			#./../pvpr/vpr ${bench_name}.net ./../pvpr/${ARCH_FILE} ${bench_name}.p ${bench_name}.r -activity_file ${bench_name}.ac2 -function_file ${bench_name}.fun -power_flag 0 -router_algorithm timing_driven -nodisp > ${bench_name}.vpr_outp
			
			#vpr ${bench_name}.net ${ARCH_FILE} ${bench_name}.p ${bench_name}.r -nodisp -full_stats -place_only -place_algorithm bounding_box > ${bench_name}.place_outp
			#END=$(date +%s)
			#this gives the end time. 
			#PLACE_TIME=$(( $END - $START ))
#in essence, the difference b/w the start n end time, i.e., the place time gives the time for the vpr execution(place algorithm only)
			#critical_path=$(awk '/Critical_Path:/ {print $2}' ./power.echo)
	 		
			#channel_width=$(awk '/Best.routing.used.a.channel.width/ {print $9}' ./${bench_name}.vpr_outp)
			
		
			echo ${bench_name},${N},${I},${alpha}, ${beta},${clustering_power},${pvpr_power},${channel_width},${energy},${critical_path}>>simulation_outputs.csv

        	fi

  done

done

exit 0
