c++ 为什么我在omnet++中的循环模拟不起作用?

ecfsfe2w  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(159)

我是omnet++的新手。为了熟悉它,我试图在一个有“n”个节点的环形拓扑中模拟一个简单的循环调度。它编译得很好,但模拟本身并不像预期的那样运行。理想情况下,它应该显示环形拓扑中的“n”个节点,并且应该从node1开始向它们发送和处理消息。这是NED文件的样子:

import ned.DelayChannel;

module Node
{
    parameters:
        double cpuClock = 500;
        double memsize = 512;
        int numCores = 1;

    gates:
          inout out[];
          connections allowunconnected:
}

network RingTopology
{
    parameters:
        int numNodes = 4; // number of nodes in the ring
        
    types:
        channel RingChannel extends ned.DelayChannel
        {
            delay = 1us;
        }

    submodules:
        node[numNodes]: Node {
            @display("p=100,100;i=device/cpu_s");
            gates: out[2];
        }

    connections allowunconnected:
        for i=0..numNodes-1 {
            node[i].out[0] <--> DelayChannel <-->  node[(i+1)%numNodes].out[1];
        }
}

这就是C++文件的样子:

#include <omnetpp.h>
#include <string.h>

using namespace omnetpp;

class RingTopology : public cSimpleModule
{

    public :
        RingTopology() : tasks {8, 6, 36, 88, 15, 14, 46, 29, 30, 5, 93, 43, 68, 65, 20, 69, 49} {}

    private:
        int numNodes;
        int cpuClock;
        const int numTasks = 17;
//        const int numTasks;
        int currentTask;
        int tasks[];

    protected:
        virtual void initialize() override;
        virtual void handleMessage(cMessage *msg) override;
};

class Node : public cSimpleModule
{
    private:
      int numNodes;
      int numCores;

    protected:
      virtual void initialize() override;
      virtual void handleMessage(cMessage *msg) override;
};

Define_Module(RingTopology);

void RingTopology::initialize()
{
    numNodes = par("numNodes");
    cpuClock  = par("cpuClock");
    currentTask = 0;
}

//void Node::handleMessage(cMessage *msg)
void RingTopology:: handleMessage(cMessage *msg)
{
    if (msg->isSelfMessage()) {
        // Round-robin scheduling
        if (currentTask < numTasks-1)
            currentTask++;
        else
            currentTask = 0;

        // Calculate time slice based on task time and CPU speed

        int SIMTIME_HZ = 1000000000;
        simtime_t cpuClock = SimTime(500.0, SIMTIME_US) * SIMTIME_HZ;
        double taskTime = tasks[currentTask] * 1.0; // convert taskTime to double
        simtime_t timeSlice = taskTime / cpuClock;  // divide taskTime by cpu clock to find the number of seconds required to finish the task

        msg->setKind(1);
        send(msg, "out", currentTask % numNodes);
        scheduleAt(simTime() + timeSlice, msg);
    }
    else {
        // Message from another node, process it
        handleMessage(msg);
    }

}
而.ini文件看起来是这样的

[General]
# nothing here

[RingTopology]
network = RingTopology

运行模拟时,模拟器窗口如下所示:

请让我知道如何解决这个问题,我做错了什么。谢谢

hc2pp10m

hc2pp10m1#

您的模拟不起作用,因为在initialize()中,您既不发送self消息,也不发送消息。
但是,即使您添加了发送缺少的消息,您的代码也会引发错误。请关注以下行:

send(msg, "out", currentTask % numNodes);
scheduleAt(simTime() + timeSlice, msg); // will raise an error

消息(更确切地说:指向cMessage对象的指针)在发送后不得使用。请参阅OMNeT++模拟手册-第4.7.3章:
消息就像一个真实世界的对象--它不能同时在两个地方。一旦发送出去,消息就不再属于模块:它被模拟内核接管,并最终被传递到目标模块。发送方模块甚至不应该再引用它的指针。

相关问题