int main(int argc, char *argv[]) { /* Check if there is enough arguments. */ if(argc < 3 || argc > 4) ExitFailure("Parameter(s): <Server Address> <String> [<Server Port>]"); /* First arg : server IP address (dotted quad) * Second arg: string to echo * Third arg : server port * If no port is provided, we used 7 since it is usually used * for testing server. */ char *servIP = argv[1]; char *echoString = argv[2]; in_port_t servPort = (argc == 4)?atoi(argv[3]):7; /* Create a reliable, stream socket using TCP. */ int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock < 0) ExitFailure("socket() failed"); /* Construct the server address structure. */ struct sockaddr_in servAddr; /* server address */ memset(&servAddr, 0, sizeof(servAddr)); /* zero out structure */ servAddr.sin_family = AF_INET; /* IPv4 address family */ /* Convert address. */ int rtnVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr); if(rtnVal == 0) ExitFailure("inet_pton() failed: invalid address string"); else if(rtnVal < 0) ExitFailure("inet_pton() failed"); servAddr.sin_port = htons(servPort); /* Server port */ /* Establish the connection to the echo server. */ if(connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) ExitFailure("connect() failed"); /* Determine the input length. */ size_t echoStringLen = strlen(echoString); /* Send the string to the server. */ ssize_t numBytes = send(sock, echoString, echoStringLen+1, 0); sleep(2); numBytes = send(sock, echoString, echoStringLen+1, 0); /* Receive the same string back from the server. */ unsigned int totalBytesRcvd = 0; fputs("Received: ", stdout); while (totalBytesRcvd < echoStringLen) { char buffer[BUFFSIZE]; /* Receive up to the buffer size (minus the null-terminated character) * bytes from the sender. */ numBytes = recv(sock, buffer, BUFFSIZE-1, 0); if(numBytes < 0) ExitFailure("recv() failed"); else if(numBytes == 0) ExitFailure("recv(): connection closed prematurely"); totalBytesRcvd += numBytes; /* Count the total of byte. */ buffer[numBytes] = '\0'; /* Terminated the string. */ fputs(buffer, stdout); /* Print the echo buffer. */ } fputc('\n', stdout); close(sock); return 0; }
int main( int argc, char *argv[] ) { FILE *output, *input; // File pointers for the data that will be read in and written out. double programRunTime; // The amount of time a this program took to complete execution. int processCount; // The total number of processes we have including PI_Main. int workerCount; // The max number of workers this program run can utilize. // Setup our program, calculate how many workers we have. processCount = PI_Configure( &argc, &argv ); workerCount = processCount - 1; PI_StartTime(); if(argc != 2){ // If after setting up our program, we don't have a string for a file argument, abort this run. return ExitFailure(", Error: No input file specified.", __FILE__, __LINE__); } // Create the workers based off worker count, and the channels/bundles between the workers and PI_Main. SetupWorkers(workerCount); //**********// PI_StartAll(); //**********// // File opening output = fopen("pal.out", "a"); input = fopen(argv[1], "r"); // File error checking. if(output==NULL){ return ExitFailure("Could not open output file 'pal.out' for appending in current directory.", __FILE__, __LINE__); } if(input==NULL){ return ExitFailure("Could not open input file for reading, from path in argv[1].", __FILE__, __LINE__); } else{ printf(">> Input file is: '%s'\n", argv[1]); } // Run serial or parallel algorithm. if(workerCount == 0){ Serial(input, output); } else{ Parallel(input, output, workerCount); } // Clean up our program run. fclose(output); fclose(input); free(toWorker); free(Worker); free(result); printf(">> Exiting... <<\n"); programRunTime = PI_EndTime(); printf("\n\n>>< PROGRAM EXECUTION TIME: (%.2f)s ><<\n\n\n", programRunTime); PI_StopMain(0); return EXIT_SUCCESS; }
/* Performs all the work in the parallel version of this program. Is not run in serial version of this program (when workerCount == 0). - Loops accepting data from PI_Main until END_OF_DATA message is recieved. - Each time data is recieved the data is processed, the results appended to the variable 'pallindromes' and then this worker tells PI_Main it is ready for more data. - When END_OF_DATA message is recieved, this worker sends PI_Main the contents of the variable 'pallindromes'. */ int workerFunc( int index, void* arg2 ) { int pallindromesCount = 0;// How many pallindromes have been found so far int maxPallindromes = 32; // The maximum number of pallindromes that can be held by this worker. This will grow dynamically to keep up as the worker finds more and more pallindromes. int startingLine = 0; // The starting line of the data sent from PI_Main. Data from PI_Main is assumed to be a continous set of lines from the input file. int dataLength = 0; // The number of chars (+ 1 for '\0') contained in the last data sent from PI_Main. int lineIndex = 0; // The current index of a temporary string, used to parse the data from PI_Main. The string this indexes is sent to function 'LineIsPalindrome()'. int curLine; // The running counter of what line we are on in the file. 'curLine' starts at startingLine, and is incremented everytime a '\n' is found in variable 'data' from PI_Main. int i; // Generic iterator variable, used to loop through the data sent from PI_Main. char line[MAX_LINE_LENGTH]; // A single line from the input file. Parsed out of the data sent from PI_Main in the variable 'data'. char *pallindromes; // The output from this worker, 'pallindromes' will be returned to PI_Main when this workers recieves END_OF_DATA as a line number. // Simply a list of all the lines in the input file that are pallindromes, seperated by newlines. char *data = NULL; // The data sent from PI_Main, LINES_BEFORE_SENDING number of lines or less. This is a continous string, each line must be parsed out by looking for '\n's. // Malloc pallindromes to a default starting size pallindromes = (char *)malloc(sizeof(char) * maxPallindromes * MAX_OUTPUT_LINE_LENGTH); while(1){ // Tell PI_Main this worker is ready for data. PI_Write( result[index], "%c", READY_FOR_DATA); // Wait until PI_Main sends this worker something. PI_Read( toWorker[index], "%d", &startingLine); // If PI_Main sends us the END_OF_DATA signal, we're done, otherwise we should work on the data recieved. if(startingLine == END_OF_DATA){ break; } // If PI_Main didn't send us END_OF_DATA, then PI_MAIN must have data for us to work on. PI_Read( toWorker[index], "%^s", &dataLength, &data ); // Once we have the data, initalize the workers line counter. curLine = startingLine; // Go through each char of the data recieved in order to parse it. for(i = 0; i < dataLength; i++){ // Copy the data from PI_Main into a single line. line[lineIndex] = data[i]; lineIndex++; // If we encounter a char that represents the end of a line, we have a line, and we should figure out if it is a pallindrome. if(data[i] == '\n' || data[i] == '\0'){ line[lineIndex - 1] = '\0';// Cap the line, so it becomes a proper C style string. if(LineIsPalindrome(line)){ if(pallindromesCount >= maxPallindromes){ // If we've run out of space to store line numbers of lines that are pallindromes, maxPallindromes = maxPallindromes * 2; // have this worker allocate more space. Double the amount of space we allocate each time in an attempt to pallindromes = (char *)realloc(pallindromes, sizeof(char) * maxPallindromes * MAX_OUTPUT_LINE_LENGTH); // balance performance and memory usage. if(pallindromes == NULL){ ExitFailure("Error reallocing...", __FILE__, __LINE__); } } // Save the line number to our workers output string, (if it is a pallindrome), and increment the number of pallindromes we've found. sprintf(&(pallindromes[pallindromesCount * MAX_OUTPUT_LINE_LENGTH]), "%010d\n", curLine); pallindromesCount++; } // Prepare to continue reading the data sent from PI_Main. curLine++; line[0] = '\0'; lineIndex = 0; } } free(data); } printf(">> Worker %d recieived end of data message\n\n", index); // Send PI_Main the results of this workers work. PI_Write( result[index], "%^s", strlen(pallindromes) + 1, pallindromes); free(pallindromes); return 0; }
int main(int argc, char *argv[]) { int opt; struct option longopts[] = { {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'v'}, {"about", 0, NULL, 'a'}, {"error", 1, NULL, 'e'}, {"log", 1, NULL, 'l'}, {"output", 1, NULL, 'o'}, {"test0", 1, NULL, '0'}, {"test1", 1, NULL, '1'}, {"test2", 1, NULL, '2'}, {"test3", 1, NULL, '3'}, {"test4", 1, NULL, '4'}, {0, 0, 0, 0} }; simulator_ *simulator = NULL; char *outFileName = NULL; FILE *outFile = stdout; double R, dc, Z0, Td, loss; double *data; char **variables; int numPoints, numVariables, i; double pulseD[7] = { 0, 10, 10e-9, 2e-9, 3e-9, 5e-9, 20e-9 }; double *pulse[7] = { &pulseD[0], &pulseD[1], &pulseD[2], &pulseD[3], &pulseD[4], &pulseD[5], &pulseD[6] }; double gaussD[7] = { 0, 3.3, 2e-9, 1e-9, 0.5e-9, 5e-9, 12e-9 }; double *gauss[7] = { &gaussD[0], &gaussD[1], &gaussD[2], &gaussD[3], &gaussD[4], &gaussD[5], &gaussD[6] }; /* Process the command line options */ while((opt = getopt_long(argc,argv,"hvae:l:01234o:",longopts,NULL)) != -1) { switch(opt) { case '0': /* Create a new simulator object */ simulator = simulatorNew(simulator); ExitFailureIf(simulator == NULL); R = 10; dc = 10; ExitFailureIf(simulatorAddResistor(simulator, "R1", "n1", "0", &R)); ExitFailureIf(simulatorAddSource(simulator, "V1", "n1", "0",'v', &dc, 0x0, NULL)); ExitFailureIf(simulatorRunOperatingPoint(simulator, &data, &variables, &numPoints, &numVariables)); print(outFile, data, variables, numVariables); /* Destroy the Scripter Object */ if(simulatorDestroy(&simulator)) { Warn("Failed to close simulator"); } free(data); free(variables); break; case '1': /* Create a new simulator object */ simulator = simulatorNew(simulator); ExitFailureIf(simulator == NULL); R = 10; Z0 = 50; Td = 15e-9; loss = 0.2; ExitFailureIf(simulatorAddResistor(simulator, "R1", "n1", "n2", &R)); ExitFailureIf(simulatorAddSource(simulator, "V1", "n1", "0",'v', NULL, 'p', pulse)); ExitFailureIf(simulatorAddTLine(simulator, "T2", "n2", "0", "n3", "0", &Z0, &Td, &loss)); ExitFailureIf(simulatorRunTransient(simulator, 0.1e-9, 50e-9, 0.0, 0, &data, &variables, &numPoints, &numVariables)); plot(outFile, data, variables, numPoints, numVariables); /* Destroy the Scripter Object */ if(simulatorDestroy(&simulator)) { Warn("Failed to close simulator"); } free(data); free(variables); break; case '2': /* Create a new simulator object */ simulator = simulatorNew(simulator); ExitFailureIf(simulator == NULL); R = 10; Z0 = 50; Td = 15e-9; loss = 0.2; ExitFailureIf(simulatorAddResistor(simulator, "R1", "n1", "n2", &R)); ExitFailureIf(simulatorAddSource(simulator, "V1", "n1", "0",'v', NULL, 'p', pulse)); ExitFailureIf(simulatorAddTLine(simulator, "T2", "n2", "0", "n3", "0", &Z0, &Td, &loss)); for(i = 0; i < 100; i++) { ExitFailureIf(simulatorRunTransient(simulator, 0.1e-9, 50e-9, 0.0, 0, &data, &variables, &numPoints, &numVariables)); } //plot(outFile, data, variables, numPoints, numVariables); /* Destroy the Scripter Object */ if(simulatorDestroy(&simulator)) { Warn("Failed to close simulator"); } free(data); free(variables); break; case '3': /* Create a new simulator object */ simulator = simulatorNew(simulator); ExitFailureIf(simulator == NULL); double L0[4] = {231.832e-9, 38.1483e-9, 38.1483e-9, 231.819e-9}; double *L0p = L0; double C0[4] = {156.163e-12, -8.60102e-12,-8.60102e-12, 156.193e-12}; double *C0p = C0; double R0[4] = {0.861113, 0, 0, 0.861113}; double *R0p = R0; double G0[4] = {0,0,0,0}; double *G0p = G0; double Rs[4] = {0.368757e-3, 0,0, 0.368757e-3}; double *Rsp = Rs; double Gd[4] = {0,0,0,0}; double *Gdp = Gd; char *nodes[6] = {"1", "3", "0", "2", "4", "0"}; int M = 9; double len = 0.0265; double fgd = 1e100; double fK = 1e9; ExitFailureIf(simulatorAddResistor(simulator, "R1", "n1", "n2", &R)); ExitFailureIf(simulatorAddSource(simulator, "V1", "n1", "0",'v', NULL, 'p', pulse)); ExitFailureIf(simulatorAddTLineW(simulator, "T1", nodes, 6, &M, &len, &L0p, &C0p, &R0p, &G0p, &Rsp, &Gdp, &fgd, &fK)); /* Destroy the Scripter Object */ if(simulatorDestroy(&simulator)) { Warn("Failed to close simulator"); } //free(data); //free(variables); break; case '4': /* Create a new simulator object */ simulator = simulatorNew(simulator); ExitFailureIf(simulator == NULL); ExitFailureIf(simulatorAddSource(simulator, "V1", "n1", "0",'v', NULL, 'g', gauss)); ExitFailureIf(simulatorRunTransient(simulator, 0.01e-9, 50e-9, 0.0, 0, &data, &variables, &numPoints, &numVariables)); plot(outFile, data, variables, numPoints, numVariables); /* Destroy the Scripter Object */ if(simulatorDestroy(&simulator)) { Warn("Failed to close simulator"); } free(data); free(variables); break; case 'a': case 'v': version(); ExitSuccess; case 'o': outFileName = optarg; outFile = fopen(outFileName, "wb"); ExitFailureIf(outFile == NULL, "Failed to open %s for output", outFileName); break; case 'e': OpenErrorFile(optarg); break; case 'l': OpenLogFile(optarg); break; case 'h': help(); ExitSuccess; case '?': ExitFailure("Unkown option"); case ':': ExitFailure("Option needs a value"); default: help(); ExitFailure("Invalid option"); } } if(outFileName != NULL) { fclose(outFile); } CloseErrorFile; CloseLogFile; ExitSuccess; }