int tftp_get_file(char *hostip,char *filename ,char *buf){ char mode[12] = "octet"; int port = 69; char opcode =1; int sock, server_len, len, opt; //,n; //struct hostent *host; /*for host information */ struct sockaddr_in server; //, client; /*the address structure for both the server and client */ /*Create the socket, a -1 will show us an error */ if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { printf ("Client: Socket could not be created"); return 0; } printf ("tftp_get_file\n"); /*set the address values for the server */ memset (&server, 0, sizeof (server)); /*Clear the structure */ server.sin_family = AF_INET; /*address family for TCP and UDP */ server.sin_addr.s_addr=inet_addr(hostip); //server.sin_addr.s_addr = htonl (INADDR_ANY); /*use any address */ server.sin_port = htons (port); /*pick a free port */ server_len = sizeof (server); /*get the length of the server address */ //memset (buf, 0, BUFSIZ); /*clear the buffer */ /* this is the first request message */ len = req_packet (opcode, filename, mode, buf); printf ("the len is %d\n",len); //printf ("the server.sin_addr is %s\n",server.sin_addr.s_addr); if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, server_len) != len) { perror ("Client: sendto has returend an error"); exit (ERROR); } strncpy (mode, "octet", sizeof (mode) - 1); tget (filename, server, mode, sock); }
int main(int argc, char *argv[]) { /* arg processing */ int nok = 0; char *s; /* args */ int flags = 0; /* argument flags */ int procid = DEFAULT_PROCID; int nprocs = DEFAULT_NPROC; int loop = DEFAULT_LOOP; ITEMPTR hosts = NULL; /* timers */ tick_t start[S_COUNT], finish[S_COUNT]; char *section[S_COUNT]; double tbuf[S_COUNT], maxbuf[S_COUNT], minbuf[S_COUNT], avgbuf[S_COUNT]; /* application */ int i; int ret; ITEMPTR hptr; fox_ptr fhand; #ifdef USE_MPI //MPI_Init(&argc, &argv); int provided; if (MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided) != MPI_SUCCESS) { fprintf(stderr, "MPI_Init_thread failed"); exit(-1); } if (provided != MPI_THREAD_MULTIPLE) { fprintf(stderr, "MPI does not provide proper thread support"); exit(-1); } MPI_Comm_rank(MPI_COMM_WORLD, &procid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); #else /* USE_MPI */ if ((s = getenv("SLURM_NPROCS")) != NULL) nprocs = atoi(s); if ((s = getenv("SLURM_PROCID")) != NULL) procid = atoi(s); #endif /* USE_MPI */ if ((s = getenv("HOSTLIST")) != NULL) add_item(&hosts, s); while (--argc > 0 && (*++argv)[0] == '-') for (s = argv[0]+1; *s; s++) switch (*s) { case 'h': flags |= HFLAG; break; case 'i': if (isdigit(s[1])) procid = atoi(s+1); else nok = 1; s += strlen(s+1); break; case 'l': if (isdigit(s[1])) loop = atoi(s+1); else nok = 1; s += strlen(s+1); break; case 'n': if (isdigit(s[1])) nprocs = atoi(s+1); else nok = 1; s += strlen(s+1); break; case 's': add_item(&hosts, s+1); s += strlen(s+1); break; case 'v': flags |= VFLAG; break; default: nok = 1; fprintf(stderr, " -- not an option: %c\n", *s); break; } if (flags & VFLAG) fprintf(stderr, "Version: %s\n", TOSTRING(VERSION)); if (nok || /*argc < 1 ||*/ (argc > 0 && *argv[0] == '?')) { fprintf(stderr, "Usage: tfox -vh -i<int> -n<int> -s<str> -t<int>\n"); fprintf(stderr, " -h halt servers\n"); fprintf(stderr, " -i process id, default: %d\n", DEFAULT_PROCID); fprintf(stderr, " -l loop count, default: %d\n", DEFAULT_LOOP); fprintf(stderr, " -n number of processes, default: %d\n", DEFAULT_NPROC); fprintf(stderr, " -s server name and port (e.g. hostname:port)\n"); fprintf(stderr, " -v version\n"); exit(EXIT_FAILURE); } /* * * * * * * * * * Initialization * * * * * * * * * */ if (hosts == NULL) add_item(&hosts, "localhost"); if (flags & VFLAG) { fprintf(stderr, "np:%d pid:%d\n", nprocs, procid); } memset(start, 0, sizeof(start)); memset(finish, 0, sizeof(finish)); fox_new(&fhand, nprocs, procid); for (hptr = hosts; hptr != NULL; hptr = hptr->next) { if (flags & VFLAG) fprintf(stderr, "%s\n", hptr->obj); fox_server_add(fhand, hptr->obj); } /* Use key-value server so that it is initialized (internal structures setup) and doesn't delay subsequent timed sections */ #define init_sz 4096 BARRIER(A) dbprintf("fox_queue_set (init)\n"); for (i = 4; i <= init_sz; i+=i) { size_t buf_sz = i; char *buf_ptr = (char *)malloc(buf_sz); char key[MAX_KEY]; memset(buf_ptr, 0xFF, buf_sz); sprintf(key, "QI:%d", procid); ret = fox_queue_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_queue_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } BARRIER(B) dbprintf("fox_queue_get (init)\n"); for (i = 4; i <= init_sz; i+=i) { size_t buf_sz; char *buf_ptr; char key[MAX_KEY]; sprintf(key, "QI:%d", procid); ret = fox_queue_get(fhand, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != i) { fprintf(stderr, " -- error: fox_queue_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } FLUSH /* * * * * * * * * * Timed Sections * * * * * * * * * */ /* * * * * * * * * * fundamental set & get * * * * * * * * * */ #define fund_sz 4 BARRIER(A) dbprintf("fox_set\n"); section[s_fox_set] = "fox_set"; { size_t buf_sz = fund_sz; char *buf_ptr = (char *)malloc(buf_sz); memset(buf_ptr, 0xFF, buf_sz); tget(start[s_fox_set]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "F:%d:%d", procid, i); ret = fox_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_set]); free(buf_ptr); } BARRIER(B) dbprintf("fox_get\n"); section[s_fox_get] = "fox_get"; { size_t buf_sz; char *buf_ptr; tget(start[s_fox_get]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "F:%d:%d", procid, i); ret = fox_get(fhand, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != fund_sz) { fprintf(stderr, " -- error: fox_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } tget(finish[s_fox_get]); } FLUSH /* * * * * * * * * * synchronization * * * * * * * * * */ #define sync_sz 4 BARRIER(A) dbprintf("fox_sync_set\n"); section[s_fox_sync_set] = "fox_sync_set"; { size_t buf_sz = sync_sz; char *buf_ptr = (char *)malloc(buf_sz); memset(buf_ptr, 0xFF, buf_sz); tget(start[s_fox_sync_set]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "S:%d:%d", procid, i); ret = fox_sync_set(fhand, 1, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_sync_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_sync_set]); free(buf_ptr); } BARRIER(B) dbprintf("fox_sync_get\n"); section[s_fox_sync_get] = "fox_sync_get"; { size_t buf_sz; char *buf_ptr; tget(start[s_fox_sync_get]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "S:%d:%d", procid, i); ret = fox_sync_get(fhand, 1, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != sync_sz) { fprintf(stderr, " -- error: fox_sync_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } tget(finish[s_fox_sync_get]); } FLUSH /* * * * * * * * * * broadcast * * * * * * * * * */ #define broad_sz 4096 BARRIER(A) if (procid == ROOT) dbprintf("fox_broad_set\n"); section[s_fox_broad_set] = "fox_broad_set"; if (procid == ROOT) { /* one broadcaster, not enough server memory at scale */ size_t buf_sz = broad_sz; char *buf_ptr = (char *)malloc(buf_sz); memset(buf_ptr, 0xFF, buf_sz); tget(start[s_fox_broad_set]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "B:%d", i); ret = fox_broad_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_broad_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_broad_set]); free(buf_ptr); } BARRIER(B) dbprintf("fox_broad_get\n"); section[s_fox_broad_get] = "fox_broad_get"; { size_t buf_sz; char *buf_ptr; tget(start[s_fox_broad_get]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "B:%d", i); ret = fox_broad_get(fhand, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != broad_sz) { fprintf(stderr, " -- error: fox_broad_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } tget(finish[s_fox_broad_get]); } FLUSH /* * * * * * * * * * reduce * * * * * * * * * */ #define reduce_sz sizeof(int) BARRIER(A) dbprintf("fox_reduce_set\n"); section[s_fox_reduce_set] = "fox_reduce_set"; { size_t buf_sz = reduce_sz; char *buf_ptr = (char *)malloc(buf_sz); memcpy(buf_ptr, &procid, sizeof(int)); tget(start[s_fox_reduce_set]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "R:%d", i); ret = fox_reduce_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_reduce_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_reduce_set]); free(buf_ptr); } BARRIER(B) if (procid == ROOT) dbprintf("fox_reduce_get\n"); section[s_fox_reduce_get] = "fox_reduce_get"; if (procid == ROOT) { /* one reducer */ size_t buf_sz = reduce_sz; char *buf_ptr = (char *)malloc(buf_sz); tget(start[s_fox_reduce_get]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "R:%d", i); memset(buf_ptr, 0, sizeof(int)); ret = fox_reduce_get(fhand, key, strlen(key), buf_ptr, buf_sz, (void (*)(void *, void *))accumulate); if (ret != 0 || *(int *)buf_ptr != nprocs*(nprocs-1)/2) { fprintf(stderr, " -- error: fox_reduce_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_reduce_get]); free(buf_ptr); } FLUSH /* * * * * * * * * * queue * * * * * * * * * */ #define queue_sz_A 4 BARRIER(A) dbprintf("fox_queue_set_A\n"); section[s_fox_queue_set_A] = "fox_queue_set_A"; { size_t buf_sz = queue_sz_A; char *buf_ptr = (char *)malloc(buf_sz); memset(buf_ptr, 0xFF, buf_sz); tget(start[s_fox_queue_set_A]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "QA:%d", procid); ret = fox_queue_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_queue_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_queue_set_A]); free(buf_ptr); } BARRIER(B) dbprintf("fox_queue_get_A\n"); section[s_fox_queue_get_A] = "fox_queue_get_A"; { size_t buf_sz; char *buf_ptr; tget(start[s_fox_queue_get_A]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "QA:%d", procid); ret = fox_queue_get(fhand, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != queue_sz_A) { fprintf(stderr, " -- error: fox_queue_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } tget(finish[s_fox_queue_get_A]); } FLUSH #define queue_sz_B (8*1024) /* test larger value lengths */ BARRIER(A) dbprintf("fox_queue_set_B\n"); section[s_fox_queue_set_B] = "fox_queue_set_B"; { size_t buf_sz = queue_sz_B; char *buf_ptr = (char *)malloc(buf_sz); memset(buf_ptr, 0xFF, buf_sz); tget(start[s_fox_queue_set_B]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "QB:%d", procid); ret = fox_queue_set(fhand, key, strlen(key), (const char *)buf_ptr, buf_sz); if (ret != 0) { fprintf(stderr, " -- error: fox_queue_set(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } } tget(finish[s_fox_queue_set_B]); free(buf_ptr); } BARRIER(B) dbprintf("fox_queue_get_B\n"); section[s_fox_queue_get_B] = "fox_queue_get_B"; { size_t buf_sz; char *buf_ptr; tget(start[s_fox_queue_get_B]); for (i = 0; i < loop; i++) { char key[MAX_KEY]; sprintf(key, "QB:%d", procid); ret = fox_queue_get(fhand, key, strlen(key), (char **)&buf_ptr, &buf_sz); if (ret != 0 || buf_sz != queue_sz_B) { fprintf(stderr, " -- error: fox_queue_get(%s) = %d\n", key, ret); exit(EXIT_FAILURE); } free(buf_ptr); } tget(finish[s_fox_queue_get_B]); } FLUSH /* * * * * * * * * * Collect Times * * * * * * * * * */ for (i = 0; i < S_COUNT; i++) tbuf[i] = tsec(finish[i], start[i]) / loop * 1000000; #ifdef USE_MPI MPI_Reduce(tbuf, maxbuf, S_COUNT, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); MPI_Reduce(tbuf, minbuf, S_COUNT, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); MPI_Reduce(tbuf, avgbuf, S_COUNT, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); #else /* USE_MPI */ memcpy(maxbuf, tbuf, sizeof(maxbuf)); memcpy(minbuf, tbuf, sizeof(minbuf)); memcpy(avgbuf, tbuf, sizeof(avgbuf)); #endif /* USE_MPI */ for (i = 0; i < S_COUNT; i++) if (avgbuf[i] != maxbuf[i]) avgbuf[i] /= nprocs; if (procid == ROOT) { printf("section | max usec | min usec | avg usec\n"); for (i = 0; i < S_COUNT; i++) printf("%-16s %12.0f %12.0f %12.0f\n", section[i], maxbuf[i], minbuf[i], avgbuf[i]); } RENDEZVOUS(TERM) /* terminate server */ if (procid == ROOT) fox_flush(fhand, (flags & HFLAG)?1:0); fox_free(fhand); #ifdef USE_MPI MPI_Finalize(); #endif /* USE_MPI */ return 0; }
#include <system/runtime/Continuation.h> /* ========================================================================= */ Continuation new_Continuation() { NEW_OBJECT(Continuation); return result; } /* ========================================================================= */ NATIVE1(Continuation_continue_) // LOGFUN; Continuation cont = (Continuation)self; Optr * ds = tget(Double_Stack); Optr arg = NATIVE_ARG(0); // restore the stack tset(_EXP_, cont->exp_stack->size + (&ds[-1])); tset(_CNT_, (threaded**)(&ds[STACK_SIZE]) - cont->cnt_stack->size); int i; for (i = 0; i < cont->exp_stack->size; i++) { ds[i] = cont->exp_stack->values[i]; } ds = (Optr *)tget(_CNT_); for (i = 0; i < cont->cnt_stack->size; i++) { ds[i] = cont->cnt_stack->values[i]; }
int main (int argc, char **argv) { /*local variables */ extern char *optarg; int sock, server_len, len, opt; //,n; char opcode, filename[196], mode[12] = "octet"; struct hostent *host; /*for host information */ struct sockaddr_in server; //, client; /*the address structure for both the server and client */ FILE *fp; /*a pointer to a file that the client will send or get from the server */ if (argc < 2) { help (argv[0]); return 0; } if (!(host = gethostbyname (argv[1]))) { perror ("Client could not get host address information"); exit (2); } /* All of the following deals with command line switches */ while ((opt = getopt (argc, argv, "dnoh:P:p:g:l:w:")) != -1) /* this function is handy */ { switch (opt) { case 'd': /* debug mode (no opts) */ debug = 1; break; case 'P': /* Port (opt required) */ port = atoi (optarg); if (debug) { printf ("Client: The port number is: %d\n", port); } break; case 'p': /* put a file on the server */ strncpy (filename, optarg, sizeof (filename) - 1); opcode = WRQ; fp = fopen (filename, "r"); /*opened the file for reading */ if (fp == NULL) { printf ("Client: file could not be opened\n"); return 0; } if (debug) { printf ("Client: The file name is: %s and can be read", filename); } fclose (fp); break; case 'g': /*get a file from the server */ strncpy (filename, optarg, sizeof (filename) - 1); opcode = RRQ; fp = fopen (filename, "w"); /*opened the file for writting */ if (fp == NULL) { printf ("Client: file could not be created\n"); return 0; } if (debug) { printf ("Client: The file name is: %s and it has been created", filename); } fclose (fp); break; case 'w': /* Get the window size */ ackfreq = atoi (optarg); if (debug) { printf ("Client: Window size is: %i\n", ackfreq); } //ackfreq = atoi (optarg); if (ackfreq > MAXACKFREQ) { printf ("Client: Sorry, you specified an ack frequency higher than the maximum allowed (Requested: %d Max: %d)\n", ackfreq, MAXACKFREQ); return 0; } else if (w_size == 0) { printf ("Client: Sorry, you have to ack sometime.\n"); return 0; } break; case 'l': /* packet length */ datasize = atoi (optarg); if (debug) { printf ("Client: Packet length is: %i bytes\n", datasize); } if (datasize > MAXDATASIZE) { printf ("Client: Sorry, you specified a data size higher than the maximum allowed (Requested: %d Max: %d)\n", datasize, MAXDATASIZE); return 0; } break; case 'h': /* Help (no opts) */ help (argv[0]); return (0); break; case 'o': strncpy (mode, "octet", sizeof (mode) - 1); if (debug) { printf ("Client: The mode is set to octet\n"); } break; case 'n': strncpy (mode, "netascii", sizeof (mode) - 1); if (debug) { printf ("Client: The mode is set to netascii\n"); } break; default: /* everything else */ help (argv[0]); return (0); break; } //end of switch } //end of while loop /*check for valid input*/ if (argc < 5 || argc > 12) { printf ("Client: wrong number of arguments: %d\n", argc); help (argv[0]); exit (ERROR); } /* Done dealing with switches and the command line*/ /*Create the socket, a -1 will show us an error */ if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { printf ("Client: Socket could not be created"); return 0; } /*set the address values for the server */ memset (&server, 0, sizeof (server)); /*Clear the structure */ server.sin_family = AF_INET; /*address family for TCP and UDP */ memcpy (&server.sin_addr, host->h_addr, host->h_length); /*set address of server taken from gethostbyname function */ //server.sin_addr.s_addr = htonl (INADDR_ANY); /*use any address */ server.sin_port = htons (port); /*pick a free port */ server_len = sizeof (server); /*get the length of the server address */ if (debug) printf ("Client: size of server_address is : %d bytes\n", server_len); memset (buf, 0, BUFSIZ); /*clear the buffer */ /* this is the first request message */ len = req_packet (opcode, filename, mode, buf); if (debug) printf ("Client: The request packt's length is: %d bytes\n", len); if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, server_len) != len) { perror ("Client: sendto has returend an error"); exit (ERROR); } if (debug) ip_port (server); switch (opcode) { case RRQ: /*read from the server, download */ tget (filename, server, mode, sock); break; case WRQ: /*write to the server, upload */ tsend (filename, server, mode, sock); break; default: printf ("Invalid opcode detected. Ignoring packet."); } close (sock); /* close the socket */ return 1; } //end of main
void _push_CNT(threaded* e) { threaded ** p = tget(_CNT_) - 1; tset(_CNT_, p); *p = e; }
void _push_EXP(Optr e) { Optr * p = tget(_EXP_) + 1; tset(_EXP_, p); *p = e; }
Optr pop_EXP() { Optr * p = tget(_EXP_); tset(_EXP_, p - 1); return *p; }