00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040 #include <mini_mpi.h>
00041 #include "mini_mpi-internal.h"
00042
00043
00044 int my_numprocs = 0;
00045 int my_taskid = 0;
00046 unsigned short my_init = 0;
00047 struct sockaddr_in head_addr;
00048 int my_socket;
00049 task_t *task_array;
00050
00051
00052 void string_to_tasks(int count, char *str, task_t *head)
00053 {
00054 int i;
00055 char *substring, *string = str;
00056 head = (task_t*)malloc(sizeof(task_t) * count);
00057
00058 for (i = 0; i < count; i++) {
00059 int id;
00060 char addr[16];
00061 substring = strstr(string, "::");
00062 if (substring == NULL)
00063 break;
00064 sscanf(substring, "::%d:%s", &id, addr);
00065 inet_aton(addr, &(head[id].ip_addr.sin_addr));
00067 string = substring + 2;
00068 }
00069 }
00070
00071
00072 int type_size(MPI_Datatype type)
00073 {
00074 int size = 0;
00075 switch (type) {
00076 case MPI_CHAR:
00077 case MPI_BYTE:
00078 size = 1;
00079 break;
00080 case MPI_UNSIGNED_SHORT:
00081 size = sizeof(unsigned short) / sizeof(char);
00082 break;
00083 case MPI_SHORT:
00084 size = sizeof(short) / sizeof(char);
00085 break;
00086 case MPI_UNSIGNED_INT:
00087 size = sizeof(unsigned int) / sizeof(char);
00088 break;
00089 case MPI_INT:
00090 size = sizeof(int) / sizeof(char);
00091 break;
00092 case MPI_UNSIGNED_LONG:
00093 size = sizeof(unsigned long) / sizeof(char);
00094 break;
00095 case MPI_LONG:
00096 size = sizeof(long) / sizeof(char);
00097 break;
00098 case MPI_FLOAT:
00099 size = sizeof(float) / sizeof(char);
00100 break;
00101 case MPI_DOUBLE:
00102 size = sizeof(double) / sizeof(char);
00103 break;
00104 case MPI_LONG_DOUBLE:
00105 size = sizeof(long double) / sizeof(char);
00106 break;
00107 case MPI_PACKED:
00108 default:
00109 size = -1;
00110 }
00111 return size;
00112 }
00113
00114
00115 int buffer_to_char(char *string, int len, void *buf, int count,
00116 MPI_Datatype type)
00117 {
00118 int i, size = 0;
00119 char tmp[64];
00120
00121 switch (type) {
00122 case MPI_CHAR:
00123 strncpy(string, (char*)buf, count > len ? len : count);
00124 size = strlen(string);
00125 break;
00126 case MPI_BYTE:
00127 size = snprintf(string, len, "%hhu", *((unsigned char*)buf));
00128 for (i = 1; i < count; i++) {
00129 int length =
00130 sprintf(tmp, ":%hhu", *((unsigned char*)buf));
00131 strncat(string, tmp, len - size);
00132 size += length;
00133 }
00134 break;
00135 case MPI_UNSIGNED_SHORT:
00136 size = snprintf(string, len, "%hu", *((unsigned short*)buf));
00137 for (i = 1; i < count; i++) {
00138 int length =
00139 sprintf(tmp, ":%hu", *((unsigned short*)buf));
00140 strncat(string, tmp, len - size);
00141 size += length;
00142 }
00143 break;
00144 case MPI_SHORT:
00145 size = snprintf(string, len, "%hd", *((short*)buf));
00146 for (i = 1; i < count; i++) {
00147 int length = sprintf(tmp, ":%hd", *((short*)buf));
00148 strncat(string, tmp, len - size);
00149 size += length;
00150 }
00151 break;
00152 case MPI_UNSIGNED_INT:
00153 size = snprintf(string, len, "%u", *((unsigned int*)buf));
00154 for (i = 1; i < count; i++) {
00155 int length =
00156 sprintf(tmp, ":%u", *((unsigned int*)buf));
00157 strncat(string, tmp, len - size);
00158 size += length;
00159 }
00160 break;
00161 case MPI_INT:
00162 size = snprintf(string, len, "%d", *((int*)buf));
00163 for (i = 1; i < count; i++) {
00164 int length = sprintf(tmp, ":%d", *((int*)buf));
00165 strncat(string, tmp, len - size);
00166 size += length;
00167 }
00168 break;
00169 case MPI_UNSIGNED_LONG:
00170 size = snprintf(string, len, "%lu", *((unsigned long*)buf));
00171 for (i = 1; i < count; i++) {
00172 int length =
00173 sprintf(tmp, ":%lu", *((unsigned long*)buf));
00174 strncat(string, tmp, len - size);
00175 size += length;
00176 }
00177 break;
00178 case MPI_LONG:
00179 size = snprintf(string, len, "%ld", *((long*)buf));
00180 for (i = 1; i < count; i++) {
00181 int length = sprintf(tmp, ":%ld", *((long*)buf));
00182 strncat(string, tmp, len - size);
00183 size += length;
00184 }
00185 break;
00186 case MPI_FLOAT:
00187 size = snprintf(string, len, "%f", *((float*)buf));
00188 for (i = 1; i < count; i++) {
00189 int length = sprintf(tmp, ":%f", *((float*)buf));
00190 strncat(string, tmp, len - size);
00191 size += length;
00192 }
00193 break;
00194 case MPI_DOUBLE:
00195 size = snprintf(string, len, "%lf", *((double*)buf));
00196 for (i = 1; i < count; i++) {
00197 int length = sprintf(tmp, ":%lf", *((double*)buf));
00198 strncat(string, tmp, len - size);
00199 size += length;
00200 }
00201 break;
00202 case MPI_LONG_DOUBLE:
00203 size = snprintf(string, len, "%Lf", *((long double*)buf));
00204 for (i = 1; i < count; i++) {
00205 int length =
00206 sprintf(tmp, ":%Lf", *((long double*)buf));
00207 strncat(string, tmp, len - size);
00208 size += length;
00209 }
00210 break;
00211 case MPI_PACKED:
00212 default:
00213 size = -1;
00214 }
00215 return size;
00216 }
00217
00218 int char_to_buffer(void *buf, int count, MPI_Datatype type, char *string)
00219 {
00220 int size = 0;
00222 return size;
00223 }
00224
00225
00226
00227
00228 int MPI_Initialized(int *flag)
00229 {
00230 *flag = my_init;
00231 return MPI_SUCCESS;
00232 }
00233
00234
00235 int MPI_Comm_size(MPI_Comm comm, int *size)
00236 {
00237 if (comm != MPI_COMM_WORLD || my_init == 0)
00238 return -1;
00239 *size = my_numprocs;
00240 return MPI_SUCCESS;
00241 }
00242
00243
00244 int MPI_Comm_rank(MPI_Comm comm, int *rank)
00245 {
00246 if (comm != MPI_COMM_WORLD || my_init == 0)
00247 return -1;
00248 *rank = my_taskid;
00249 return MPI_SUCCESS;
00250 }