+ msg_file_priv_t file_priv = MSG_file_priv(fd);
+ sg_size_t write_size;
+
+ /* Find the host where the file is physically located (remote or local)*/
+ msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
+ msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
+ msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname);
+
+ if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
+ /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
+ XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
+ msg_host_t *m_host_list = NULL;
+ m_host_list = calloc(2, sizeof(msg_host_t));
+
+ m_host_list[0] = MSG_host_self();
+ m_host_list[1] = attached_host;
+ double computation_amount[] = { 0, 0 };
+ double communication_amount[] = { 0, (double)size, 0, 0 };
+
+ msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, computation_amount, communication_amount, NULL);
+ msg_error_t transfer = MSG_parallel_task_execute(task);
+ MSG_task_destroy(task);
+ free(m_host_list);
+ if(transfer != MSG_OK){
+ if (transfer == MSG_HOST_FAILURE)
+ XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
+ if (transfer == MSG_TASK_CANCELED)
+ XBT_WARN("Transfer error, task has been canceled!");
+
+ return -1;
+ }
+ }
+ /* Write file on local or remote host */
+ write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
+
+ return write_size;