Example #1
0
func main() {

	flag.Parse()

	_ints = *intsFlag

	fmt.Println("Starting Application")

	fmt.Println(fmt.Sprintf("Processing using Parameters: -n %d -o %s", *intsFlag, *outputFlag))

	rand.Seed(time.Now().UTC().UnixNano())

	var m []int = make([]int, _ints)

	fmt.Println("Generating List")

	utility.GenerateList(&m)

	startTime := time.Now()

	fmt.Println("Sorting List")

	sort.Ints(m)

	endTime := time.Since(startTime)
	fmt.Println("Total Elapsed Time:", time.Since(startTime))
	fmt.Println("All Lists Done Sorting")

	fmt.Println("Verifying List is sorted.")

	isSorted := true

	for i := 0; i < len(m)-1; i++ {
		if m[i] > m[i+1] {
			isSorted = false
			i = len(m)
		}
	}

	if isSorted == true {
		fmt.Println("List verified as sorted in", time.Since(startTime))
	} else {
		fmt.Println("****LIST FIALED VERIFICATION. LIST IS NOT SORTED.*********")
	}

	fmt.Println("Writing output")
	writeOutput(fmt.Sprintf("%d,%d\r\n", _ints, endTime), *outputFlag)
}
func main() {

	flag.Parse()

	_ints = *intFlag

	fmt.Println("Starting Application")

	fmt.Println(fmt.Sprintf("Processing using Parameters: -n %d -s %d -clf %s", *intFlag, *countFlag, *clientList))

	runtime.GOMAXPROCS(_threads)

	rand.Seed(time.Now().UTC().UnixNano())

	//generates lists

	var m []int = make([]int, _ints)

	fmt.Println("Generating List")

	utility.GenerateList(&m)

	/*******opens clients based on list*********/

	clients := ReadClientList(*clientList)

	clientSize := len(clients)

	if clientSize == 0 {
		fmt.Println("PANIC: NO DATA SERVERS LOADED.\nUsed Server File: ", *clientList)
		os.Exit(2)
	}

	fmt.Println("Loaded ", clientSize, " data servers from "+*clientList+".")

	if *countFlag > len(clients) || *countFlag == 0 {
		*countFlag = len(clients)
	}

	if clientSize != *countFlag {
		clientSize = *countFlag
		fmt.Println("Using", clientSize, " data servers, as per cli.")
	}

	var activeClientCount int = 0
	var activeClientList []string = make([]string, clientSize)

	for _, val := range clients {

		var err error

		_, err = rpc.DialHTTP("tcp", val)

		if err != nil {
			fmt.Println("Client Not Available: ", err)
		} else {
			fmt.Println("Client Available at " + val)
			activeClientList[activeClientCount] = val
			activeClientCount++
		}
	}

	if activeClientCount == 0 {
		fmt.Println("No active Clients found.")
		fmt.Println("Exiting Application.")
		os.Exit(1)
	}
	list := make([][]int, activeClientCount)

	clients = activeClientList[:activeClientCount]

	l2 := make([][]int, len(list))

	startTime := time.Now()
	s := time.Now()
	fmt.Println("Start Time:", startTime)

	chans := make([]chan []int, clientSize)

	for i, _ := range chans {
		chans[i] = make(chan []int)
	}

	for i := 0; i < *countFlag; i++ {

		if len(strings.TrimSpace(clients[i])) > 0 {
			c := chans[i]
			go SortClient(&m, &l2[i], &list[i], clients[i], i, clientSize, c)
		} else {
			close(chans[i])
		}
	}

	allDone := false
	ok := make([]bool, clientSize)

	for allDone == false {
		allDone = true

		for i, val := range chans {
			data, isNotDone := <-val
			ok[i] = isNotDone
			if ok[i] == true {
				allDone = false
				l2[i] = make([]int, len(data))
				for k, v3 := range data {
					l2[i][k] = v3
				}
			}
		}
	}

	sortTime := time.Since(startTime)
	fmt.Println("List Split and Sorted in ", time.Since(startTime))

	startTime = time.Now()

	fmt.Println("Combining sorted lists")

	listLength := 0

	for i, _ := range l2 {
		listLength += len(l2[i])
	}
	fmt.Println(listLength)

	m = utility.CombineSortedLists(l2)
	fmt.Println("List Combined in ", time.Since(startTime))
	combineTime := time.Since(startTime)

	startTime = time.Now()
	fmt.Println("Verifying List of size", len(m), "is sorted.")

	//verifies list is sorted.
	isSorted := true
	for i := 0; i < len(m)-1; i++ {
		if m[i] > m[i+1] {
			isSorted = false
			i = len(m)
		}
	}

	if isSorted == true {
		fmt.Println("List verified as sorted in", time.Since(startTime))
	} else {
		fmt.Println("****LIST FIALED VERIFICATION. LIST IS NOT SORTED.*********")
	}

	fmt.Println("Total Time:", time.Since(s))
	fmt.Println("Writing output to ", outputFile)
	writeOutput(fmt.Sprintf("%d,%d,%d,%d\n", clientSize, _ints, sortTime.Nanoseconds(), combineTime.Nanoseconds()), outputFile)
}
Example #3
0
func main() {

	flag.Parse()

	if *threadFlag > 0 {
		_threads = *threadFlag
	}

	_ints = *intsFlag

	fmt.Println("Starting Application")

	//Tells GO to use the number of threads supplied.
	runtime.GOMAXPROCS(_threads)

	fmt.Println(fmt.Sprintf("Processing using Parameters: -n %d -t %d -o %s", *intsFlag, _threads, *outputFlag))

	//seeds random number generator
	rand.Seed(time.Now().UTC().UnixNano())

	var m []int = make([]int, _ints)

	fmt.Println("Generating List")

	//Step 1: Generate List
	utility.GenerateList(&m)

	startTime := time.Now()

	fmt.Println("Sorting List")

	u := new(utility.Util)

	s := ""

	//sets threads for each utility method.  When the goroutines are called,
	//they'll need this to know what to use.
	u.SetThreads(_threads, &s)

	r := new(utility.Reply)

	//sorts the list and sends back a reply with the sort time and combine time
	u.Sort(m, r)

	//copies the sorted data back into the original list.
	for i, val := range r.Data {
		m[i] = val
	}

	fmt.Println("Total Elapsed Time:", time.Since(startTime))
	fmt.Println("All Lists Done Sorting")

	startTime = time.Now()
	fmt.Println("Verifying List is sorted.")

	isSorted := true

	for i := 0; i < len(m)-1; i++ {
		if m[i] > m[i+1] {
			isSorted = false
			i = len(m)
		}
	}

	if isSorted == true {
		fmt.Println("List verified as sorted in", time.Since(startTime))
	} else {
		fmt.Println("****LIST FIALED VERIFICATION. LIST IS NOT SORTED.*********")
	}

	writeOutput(fmt.Sprintf("%d,%d,%d,%d\n", _threads, _ints, r.SortTime.Nanoseconds(), r.CombineTime.Nanoseconds()), *outputFlag)

}