1 | /* |
---|
2 | * put-tree.c - put the MP trees found in the input file to the slave |
---|
3 | * (get-tree) to be printed to a file. |
---|
4 | * |
---|
5 | * See also get-tree.c |
---|
6 | */ |
---|
7 | |
---|
8 | #include <sys/types.h> |
---|
9 | #include <fcntl.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include "pvm3.h" |
---|
12 | #include "gmp.h" |
---|
13 | #include "MP.h" |
---|
14 | #include "MP_PvmTransp.h" |
---|
15 | |
---|
16 | EXTERN MP_TranspOps_t pvm_ops; |
---|
17 | |
---|
18 | #define ENCODING PvmDataRaw |
---|
19 | #define SLAVENAME "get-tree" |
---|
20 | |
---|
21 | #include "pvm_imp_getdata.c" |
---|
22 | |
---|
23 | /********************************************************************** |
---|
24 | * FUNCTION: imp_file2tree |
---|
25 | * ARGUMENT: link - pointer to a stream structure |
---|
26 | * fname - If not empty, this is a string giving the name |
---|
27 | * of the input file (as given from the command line). |
---|
28 | * If the string is empty, the routine asks for a |
---|
29 | * filename. If the filename cannot be opened for |
---|
30 | * reading we print an error message and croak. |
---|
31 | * stid - tids to which to send the data. |
---|
32 | * RETURN: None. |
---|
33 | * PURPOSE: Read successive trees from an input file and send them to |
---|
34 | * the data stream associated with link. |
---|
35 | * |
---|
36 | * COMMENT: Note that MP_EndMsg() is called after writing each |
---|
37 | * tree to the buffer. This flushes the buffer to the |
---|
38 | * network and sets the last fragment bit in the buffer |
---|
39 | * header. The receiver must do a corrsponding m_skiprecord() |
---|
40 | * PRIOR to retrieving EACH tree from the data stream. |
---|
41 | **********************************************************************/ |
---|
42 | void |
---|
43 | #ifdef __STDC__ |
---|
44 | imp_file2tree(MP_Link_pt link, char *fname, int *stid ) |
---|
45 | #else |
---|
46 | imp_file2tree(link, fname, stid) |
---|
47 | MP_Link_pt link; |
---|
48 | char *fname; |
---|
49 | int *stid; |
---|
50 | #endif |
---|
51 | { |
---|
52 | FILE *fd; |
---|
53 | int more = 0; |
---|
54 | |
---|
55 | if (fname == NULL) { |
---|
56 | fname = malloc(64); |
---|
57 | printf("Enter name of input file: "); |
---|
58 | scanf("%s", fname); |
---|
59 | } |
---|
60 | |
---|
61 | if ((fd = fopen(fname, "r")) == NULL) { |
---|
62 | fprintf(stderr, "Can't open %s for reading\n", fname); |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | do { |
---|
67 | pvm_initsend(PvmDataRaw); |
---|
68 | /* pvm_initsend(PvmDataInPlace);*/ |
---|
69 | more = imp_get_term(link, fd); |
---|
70 | MP_EndMsg(link); |
---|
71 | if (pvm_send(*stid, 1)) { |
---|
72 | fprintf(stderr, "can't send to \"%s\"\n", SLAVENAME); |
---|
73 | goto bail; |
---|
74 | } |
---|
75 | MP_ResetLink(link); |
---|
76 | } while (more); |
---|
77 | free (fname); |
---|
78 | return; |
---|
79 | |
---|
80 | bail: |
---|
81 | pvm_exit(); exit(1); |
---|
82 | free (fname); |
---|
83 | fclose(fd); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | main(argc, argv) |
---|
89 | int argc; |
---|
90 | char *argv[]; |
---|
91 | { |
---|
92 | int mytid; /* my task id */ |
---|
93 | int stid; |
---|
94 | int i, n = 0; |
---|
95 | int spawn_flag = PvmTaskDefault; |
---|
96 | char *infile, *rhost = NULL; |
---|
97 | MP_Link_pt link = NULL; |
---|
98 | MP_Env_pt env = NULL; |
---|
99 | |
---|
100 | if ((infile = IMP_GetCmdlineArg(argc, argv, "-infile")) == NULL) { |
---|
101 | fprintf(stderr, "missing -infile argument!\n"); |
---|
102 | exit (1); |
---|
103 | } |
---|
104 | pvm_setopt(PvmRoute, PvmRouteDirect); |
---|
105 | pvm_setopt(PvmAutoErr, 0); /* tell PVM not to print error to stdout */ |
---|
106 | mytid = pvm_mytid(); /* enroll in pvm */ |
---|
107 | |
---|
108 | env = MP_AllocateEnv(); |
---|
109 | MP_SetEnvOption(env, MP_BUFFER_SIZE_OPT, pvm_getopt(PvmFragSize)); |
---|
110 | env = MP_InitializeEnv(env); |
---|
111 | if (env == NULL) { |
---|
112 | fprintf(stderr, "%s: MP_InitializeEnv() failed!\n", argv[0]); |
---|
113 | exit(1); |
---|
114 | } |
---|
115 | MP_AddEnvTranspDevice(env, MP_PvmTransportDev, &pvm_ops); |
---|
116 | |
---|
117 | if ((link = MP_OpenLink(env, argc, argv)) == NULL) { |
---|
118 | fprintf(stderr, "%s: MP_OpenLink() failed!\n", argv[0]); |
---|
119 | exit(1); |
---|
120 | } |
---|
121 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_INIT_EVENTS); |
---|
122 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_WRITE_EVENTS); |
---|
123 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_READ_EVENTS); |
---|
124 | /* need the following to use PvmDataInPlace encoding mechanism |
---|
125 | MP_SetLinkOption(link, MP_LINK_SEND_MODE_OPT, MP_SEND_WHOLE_MSG);*/ |
---|
126 | |
---|
127 | if ((mytid = pvm_mytid()) < 0) exit(1); |
---|
128 | |
---|
129 | rhost = IMP_GetCmdlineArg(argc, argv, "-remhost"); |
---|
130 | if (rhost != NULL) spawn_flag = PvmTaskHost; |
---|
131 | /* start up slave task */ |
---|
132 | if (pvm_spawn(SLAVENAME, argv, spawn_flag, rhost, 1, &stid) < 0 || stid < 0) { |
---|
133 | fputs("can't initiate slave\n", stderr); |
---|
134 | goto bail; |
---|
135 | } |
---|
136 | /* Wait for slave task to start up */ |
---|
137 | pvm_recv(stid, 0); |
---|
138 | |
---|
139 | imp_file2tree(link, infile, &stid); /* do the work */ |
---|
140 | |
---|
141 | MP_CloseLink(link); /* cleanup and exit */ |
---|
142 | MP_ReleaseEnv(env); |
---|
143 | pvm_exit(); |
---|
144 | exit(0); |
---|
145 | bail: |
---|
146 | if (stid > 0) |
---|
147 | pvm_kill(stid); |
---|
148 | pvm_exit(); |
---|
149 | exit(0); |
---|
150 | |
---|
151 | } |
---|