Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0625a1551d7d20657f89f6be4cb9d0bb9b967686
[simgrid.git] / examples / msg / icomms / peer2.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
15
16 int sender(int argc, char *argv[]);
17 int receiver(int argc, char *argv[]);
18
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 /** Sender function  */
22 int sender(int argc, char *argv[])
23 {
24   long number_of_tasks = atol(argv[1]);
25   double task_comp_size = atof(argv[2]);
26   double task_comm_size = atof(argv[3]);
27   long receivers_count = atol(argv[4]);
28
29   msg_comm_t *comm = malloc((number_of_tasks+receivers_count)*sizeof(msg_comm_t));
30   int i;
31   m_task_t task = NULL;
32   for (i = 0; i < number_of_tasks; i++)
33   {
34         char mailbox[256];
35         char sprintf_buffer[256];
36     sprintf(mailbox,"receiver-%ld",i % receivers_count);
37     sprintf(sprintf_buffer, "Task_%d", i);
38     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
39     comm[i]=MSG_task_isend(task, mailbox);
40     INFO2("Send to receiver-%ld Task_%d",i % receivers_count,i);
41   }
42   for (i = 0; i < receivers_count; i++)
43   {
44     char mailbox[80];
45     sprintf(mailbox,"receiver-%ld",i % receivers_count);
46     task = MSG_task_create ("finalize", 0, 0, 0);
47     comm[i+number_of_tasks]=MSG_task_isend(task, mailbox);
48     INFO1("Send to receiver-%ld finalize",i % receivers_count);
49
50   }
51   /* Here we are waiting for the completion of all communications*/
52   MSG_comm_waitall(comm,(number_of_tasks+receivers_count),-1);
53
54   INFO0("Goodbye now!");
55   return 0;
56 } /* end_of_sender */
57
58 /** Receiver function  */
59 int receiver(int argc, char *argv[])
60 {
61   m_task_t task = NULL;
62   MSG_error_t res;
63   int id = -1;
64   char mailbox[80];
65   msg_comm_t res_irecv;
66   xbt_assert1(sscanf(argv[1],"%d", &id),
67          "Invalid argument %s\n",argv[1]);
68   MSG_process_sleep (10);
69   sprintf(mailbox,"receiver-%d",id);
70   while(1) {
71         res_irecv = MSG_task_irecv(&(task), mailbox);
72         INFO0("Wait to receive a task");
73         res = MSG_comm_wait(res_irecv,-1);
74         xbt_assert0(res == MSG_OK, "MSG_task_get failed");
75         INFO1("Received \"%s\"", MSG_task_get_name(task));
76         if (!strcmp(MSG_task_get_name(task),"finalize")) {
77           MSG_task_destroy(task);
78           break;
79     }
80
81     INFO1("Processing \"%s\"", MSG_task_get_name(task));
82     MSG_task_execute(task);
83     INFO1("\"%s\" done", MSG_task_get_name(task));
84     MSG_task_destroy(task);
85     task = NULL;
86   }
87   INFO0("I'm done. See you!");
88   return 0;
89 } /* end_of_receiver */
90
91 /** Test function */
92 MSG_error_t test_all(const char *platform_file,
93                             const char *application_file)
94 {
95   MSG_error_t res = MSG_OK;
96
97   /* MSG_config("workstation/model","KCCFLN05"); */
98   {                             /*  Simulation setting */
99     MSG_set_channel_number(0);
100     MSG_create_environment(platform_file);
101   }
102   {                            /*   Application deployment */
103     MSG_function_register("sender", sender);
104     MSG_function_register("receiver", receiver);
105     MSG_launch_application(application_file);
106   }
107   res = MSG_main();
108   
109   INFO1("Simulation time %g",MSG_get_clock());
110   return res;
111 } /* end_of_test_all */
112
113
114 /** Main function */
115 int main(int argc, char *argv[])
116 {
117   MSG_error_t res = MSG_OK;
118
119   MSG_global_init(&argc,argv);
120   if (argc < 3) {
121      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
122      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
123      exit(1);
124   }
125   res = test_all(argv[1],argv[2]);
126    SIMIX_message_sizes_output("toto.txt");
127   MSG_clean();
128
129   if(res==MSG_OK)
130     return 0;
131   else
132     return 1;
133 } /* end_of_main */